Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
coffeescript实例变量未定义_Coffeescript_Instance Variables - Fatal编程技术网

coffeescript实例变量未定义

coffeescript实例变量未定义,coffeescript,instance-variables,Coffeescript,Instance Variables,在类PostsPager的构造函数中,我将@page的默认值设置为0,然后调用render。然而,当我进入render时,@page现在是未定义的。为什么会这样 class PostsPager contructor: (@page=0)-> render() $(window).scroll(@check) check: => if @nearBottom() @render render: => alert @pa

在类
PostsPager
的构造函数中,我将
@page
的默认值设置为0,然后调用render。然而,当我进入render时,@page现在是未定义的。为什么会这样

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);
    };

你的拼写很差,
constructor
是构造函数方法,
constructor
只是拼写很差的方法。您还需要说
@render()
来调用
render
,作为
上的方法。你想要:

class PostsPager
  constructor: (@page=0)->
    @render()
    #...
演示:


这个拼写错误意味着您的
构造函数
从未被调用,这就是为什么您从未看到由缺少的
@

@引起的“未知渲染变量”错误。克里斯:没什么,只要等到您开始犯真正的错误:)