Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 在Array.map()中调用类方法_Javascript - Fatal编程技术网

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); },这个);//添加了这个 }