在我的類PostsPager
的構造函數中,我將默認值@page
設置爲0,然後調用render。但是,當我輸入渲染時,@page現在是未定義的。爲什麼會這樣?coffeescript實例變量undefined
class PostsPager
contructor: (@page=0)->
render()
$(window).scroll(@check)
check: =>
if @nearBottom()
@render
render: =>
alert @page # @page = undefined
笏
編輯
編譯JS
PostsPager = (function() {
function PostsPager() {
this.renderPosts = __bind(this.renderPosts, this);
this.nearBottom = __bind(this.nearBottom, this);
this.render = __bind(this.render, this);
this.check = __bind(this.check, this);
}
PostsPager.prototype.contructor = function(page) {
this.page = page != null ? page : 0;
this.render();
return $(window).scroll(this.check);
};
PostsPager.prototype.check = function() {
if (this.nearBottom()) {
return this.render;
}
};
PostsPager.prototype.render = function() {
alert(this.page);
this.page++;
$(window).unbind('scroll', this.check);
return $.getJSON($('#feed').data('json-url'), {
page: this.page
}, this.renderPosts);
};
'new PostsPager()' – Chris