我创建了一个 JavaScript 对象
var Article = function(data) {
this.foo = data,
this.get_more_data = function() {
// do something, get a response
show_data(response);
},
this.show_data = function(bar) {
//do something with bar;
}
};
当 show_data 方法没有这样编写时,它可以正常工作。但在对象之外无法访问它。有了这个。我从 Chrome 控制台收到“Uncaught ReferenceError”。
这是为什么?
谢谢。
最佳答案
您应该将 show_data
作为 this
的方法来调用,而不是作为作用域为当前上下文的函数:
var Article = function(data) {
this.foo = data,
this.get_more_data = function() {
// do something, get a response
this.show_data(this.foo);
},
this.show_data = function(bar) {
console.log(bar);
}
};
关于javascript - Uncaught ReferenceError : "method" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127224/