Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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
Javascript TypeError:this.reload不是一个函数_Javascript_Jquery_Oop - Fatal编程技术网

Javascript TypeError:this.reload不是一个函数

Javascript TypeError:this.reload不是一个函数,javascript,jquery,oop,Javascript,Jquery,Oop,我的代码: Box = (function() { function Box(options) { this.id = options.id; $('#box-reload-' + this.id).click(function() { this.reload(); }); } Box.prototype.reload = function() { alert('test'); }; return Box; })(); b =

我的代码:

Box = (function() {
  function Box(options) {
    this.id = options.id;
    $('#box-reload-' + this.id).click(function() {
      this.reload();
    });
  }

  Box.prototype.reload = function() {
    alert('test');
  };

  return Box;
})();
b = new Box({'id': '0'})
单击事件已正确注册,但单击“重新加载”按钮时,出现以下错误:
TypeError:this.reload不是一个函数


如何修复此错误?

中单击
处理程序引用元素
#框重载-…
,因此使用
\u此
变量作为对正确范围的引用,例如

Box = (function() {
  function Box(options) {
    var _this = this;
    this.id = options.id;
    $('#box-reload-' + this.id).click(function() {
      _this.reload();
    });
  }

单击中
处理程序引用元素
#box reload-…
,因此使用
\u此
变量作为对正确范围的引用,例如

Box = (function() {
  function Box(options) {
    var _this = this;
    this.id = options.id;
    $('#box-reload-' + this.id).click(function() {
      _this.reload();
    });
  }

因为这是元素的范围,而不是函数。对控制台的简单测试将告诉您

console.log(this)

在外部定义一个变量来保存范围

  function Box(options) {
    var that = this;
    this.id = options.id;
    $('#box-reload-' + this.id).click(function() {
      that.reload();
    });
  }

因为这是元素的范围,而不是函数。对控制台的简单测试将告诉您

console.log(this)

在外部定义一个变量来保存范围

  function Box(options) {
    var that = this;
    this.id = options.id;
    $('#box-reload-' + this.id).click(function() {
      that.reload();
    });
  }

作品谢谢你的解释!作品谢谢你的解释!