node.jsで私はクラス/オブジェクトと 'this'キーワードで苦労します。例えば:私はこのような.sort
フラグメント削除する場合node.jsに汎用メソッドを定義する方法は?
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)
しかし、:
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 ...
function Set(arr,leq) {
this.leq = leq ? leq : function(x,y) {return x<=y;};
this.arr=arr.slice();
}
を私はして問題がありませんだから:私はどのように最初のメソッドにアクセスする必要があるときに同じオブジェクト内の別のメソッド(leq
など)にアクセスできるオブジェクト内にメソッド(geq
など)を挿入します。 sort
は機能しますか?
純粋なJavaScriptの質問ではありませんか?提案するretag --node.js – Paul