私はあなたがすべてを忘れる日を過ごしているに違いありません:)
@NilColorの答えは正しいですが、私はそれを知っていましたが、正しい帽子をかぶって考えていませんでした。
とにかく、オブジェクトにアタッチするときに必要な特定性が少し低いラッパーを使用するというアイデアがまだ好きだと判断しました。そして、少し冗長です。
ということで、オリジナルの考え方で書いてみましたので、気に入っていただけると幸いです。
myObj.wrap = function(path, context){
var wrapper,
method = ( typeof path != 'string' && context )? path : this,
context = (typeof path === 'object' && context === undefined)?
path : (context || this);
if (typeof path === 'string'){
path = path.split('.');
for ( var i = 0; i < path.length; i++ ){
method = method[path[i]];
if ( context === true )
context = ( i === path.length - 2 )? method : context;
};
};
wrapper = function(){
method.apply(context, arguments);
};
return wrapper;
}
利用方法:
ネストされたメソッドをいくつでも myObj にバインドします
myObj.wrap('foo') //binds myObj.foo to myObj
// or
myObj.wrap('foo.bar') //binds myObj.foo.bar to myObj
//or if myObj is a function
myFunc.wrap() // binds myFunc to myFunc
myObj のメソッドを別のオブジェクトにバインドする
myObj.wrap('foo.bar', someObj) //binds myObj.foo.bar to someObj
//or if myObj is a function
myFunc.wrap(someObj) //Binds myFunc to someObj
ネストされたメソッドをその親にバインドします
myObj.wrap('foo.bar', true) // binds myObj.foo.bar to myObj.foo
助っ人として使う
myObj.wrap(someFunc, someObj) //binds someFunc to someObj
メソッドバインディングのコンテキストではなく、元の質問への回答を探している場合。
myObj.getProps = function(path, context){
var context = context || this;
path = path.split('.');
for ( var i = 0; i < path.length; i++ ){
context = context[path[i]];
};
return context;
};
使用法:
オブジェクトにアタッチするか、スタンドアロン関数として
プロパティを取得する
myObj.getProps('foo.bar') // returns mayObj.foo.bar
コンテキストオブジェクトを与える
myObj.getProps('user.name', myAccounts) // returns myAccounts.user.name
スタンドアロン関数として使用するには
myObj.getProps = function(path,context){....}
と
function getProps(path,context){....}
ノート
スタンドアロン関数として使用する場合は、 のスコープから検索を開始することを覚えておく必要がありますthis
。したがって、グローバル スコープで定義されている場合は、フル パスを指定する必要があります。
例えば
getProps('myObj.foo.bar')
コンテキスト セレクターを使用して参照オブジェクトを変更することもできます。