Javascript 未捕获的TypeError*不是函数
我正在尝试使用Object.prototype编写和学习,我得到的错误是,当我在另一个方法中调用它时,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
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));
})();