node.js では、クラス/オブジェクトと「this」キーワードに苦労しています。例えば:
function Set(arr,leq) {
this.leq = leq ? leq : function(x,y) {return x<=y;};
this.arr=arr.slice().sort(this.geq);
}
Set.prototype.geq=function(x,y) { return this.leq(y,x);}
var myset = new Set([1,5,3,2,7,9]);
TypeError: Object #<Object> has no method 'leq'
at [object Context]:1:47
at Array.sort (native)
at new Set ([object Context]:3:22)
at [object Context]:1:13
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
.sort
ただし、次のようにフラグメントを削除すると:
function Set(arr,leq) {
this.leq = leq ? leq : function(x,y) {return x<=y;};
this.arr=arr.slice();
}
私は問題ありません:
var myset = new Set([1,5,3,2,7,9]);
myset.geq(3,4)
false
それでも:
myset.arr.sort(myset.geq)
TypeError: Object #<Object> has no method 'leq'
at ...
では、関数などの最初のメソッドにアクセスする必要がある場合、同じオブジェクト内のgeq
別のメソッド ( など) にアクセスできるメソッド ( など) をオブジェクトに作成するにはどうすればよいですか?leq
sort