2

This is what I want to have:

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
   });
}

But I get error with it:

Uncaught TypeError: Object [object Window] has no method 'empty_cell' 

Is it possible to call MyClass methods in the anonymous function?

4

1 に答える 1

4

this次のように、スコープを 2 番目のパラメーターとしてmap関数に渡すことができます。

MyClass.prototype.another_method = 関数() {
   this.another.map(function(el) {
     this.some_method(el);
   }、 これ); // これを追加
}

于 2012-08-14T10:46:00.707 に答える