这就是我想要的:
function MyClass() {
this.some = new Array();
this.another = new Array();
}
MyClass.prototype.some_method = function(element) {
// some work with this.some
}
MyClass.prototype.another_method = function() {
this.another.map(function(el) {
this.some_method(el); // error code
});
}
但是我得错了:
Uncaught TypeError: Object [object Window] has no method 'empty_cell'
是否可以在匿名函数中调用MyClass方法?
答案 0 :(得分:3)
您可以将this
范围作为第二个参数传递给map函数,如下所示:
MyClass.prototype.another_method = function() { this.another.map(function(el) { this.some_method(el); }, this); // added this }