Javascript 在Array.map()中调用类方法
这就是我想要的:Javascript 在Array.map()中调用类方法,javascript,Javascript,这就是我想要的: 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(func
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方法?您可以将此范围作为第二个参数传递给函数,如下所示: MyClass.prototype.another_method = function() { this.another.map(function(el) { this.some_method(el); }, this); // added this } MyClass.prototype.Other_方法=函数(){ this.other.map(函数(el){ 这是一种新的方法(el); },这个);//添加了这个 }
您可以将此范围作为第二个参数传递给函数,如下所示: MyClass.prototype.another_method = function() { this.another.map(function(el) { this.some_method(el); }, this); // added this } MyClass.prototype.Other_方法=函数(){ this.other.map(函数(el){ 这是一种新的方法(el); },这个);//添加了这个 }