Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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*不是函数_Javascript_Jquery_Oop - Fatal编程技术网

Javascript 未捕获的TypeError*不是函数

Javascript 未捕获的TypeError*不是函数,javascript,jquery,oop,Javascript,Jquery,Oop,我正在尝试使用Object.prototype编写和学习,我得到的错误是,当我在另一个方法中调用它时,this.issue不是一个函数。我做错了什么,导致它通过这个未捕获的类型错误 Javascript: $(document).ready(function() { var Example = function() { this.number1 = null; this.number2 = null; }; Example.prototype.is

我正在尝试使用Object.prototype编写和学习,我得到的错误是,当我在另一个方法中调用它时,
this.issue
不是一个函数。我做错了什么,导致它通过这个未捕获的类型错误

Javascript:

$(document).ready(function() {
   var Example = function() {
       this.number1 = null;
       this.number2 = null;
   };

   Example.prototype.issue = function() {
       //Logic is here...
    };

   Example.prototype.updateNumber = function() {
       //more logic is here...
       this.issue();
   };

   Example.prototype.updateSign = function() {
       //logic here...
       this.issue();
   };

   (function() {
       var solution = new Example();

   })();

});

更新:

在附加到
更改
事件的
#sign
.number

Example.prototype.newNumber = function(event) {
  if (event.currentTarget.id === 'number1') {
      this.number1 = parseFloat($(event.currentTarget).val()); 
  }
  else { 
     this.number2 = parseFloat($(event.currentTarget).val()); 
  }
  this.issue();
};

Example.prototype.newSign = function(event) {
    this.sign = $(event.currentTarget).val();
    this.issue();
};
引用的是
#符号
编号
元素,而不是
新示例
创建的对象

var problem = new Example();
尝试使用将
设置为
新示例()
问题
.change()中

(function() {

    var problem = new Example();

    $("#sign").change(problem.newSign.bind(problem));

    $(".number").change(problem.newNumber.bind(problem));

})();
JSFIDLE


或者,使用


jsfiddle

您能在jsfiddle中复制它吗?这将使查找问题变得更容易,因为这里充满了“伪代码”,并且您所展示的真实代码部分是有效的,不可能获得answer@Pikamander2我现在就创建一个,您在哪里调用了
issue()
?如果在您的生活中调用
solution.issue()
solution.updateNumber()
solution.updateSign()
,则一切正常。@JasmineOT我在.updateNumber&.updateSign中调用此.issue()。另外,我刚刚附上了一个JSFIDDLE,您可能想进一步说明为什么有必要这样做。@FelixKling请参阅更新的帖子谢谢@guest271314的回答和解释!
(function() {
    var problem = new Example();

    $("#sign").change($.proxy(problem.newSign, problem));

    $(".number").change($.proxy(problem.newNumber, problem));

})();