jQueryソース:
proxy: function(fn, context) {
var tmp, args, proxy;
if (typeof context === "string") {
tmp = fn[ context ];
context = fn;
fn = tmp;
}
// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
if (!jQuery.isFunction(fn)) {
return undefined;
}
// Simulated bind
args = core_slice.call(arguments, 2);
proxy = function() {
return fn.apply(context || this, args.concat(core_slice.call(arguments)));
};
// Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
},
あなたは(私は正しくスライスコードを翻訳思う)キャッシュコードを削除して、それは少し短い、あなたは本質的.apply()
をプレゼント場合:
proxy: function(fn, context) {
var args = [].slice.call(arguments, 2);
return function() {
return fn.apply(context || this, args.concat([].slice.call(arguments)));
};
}
Google-fuの便利な検索キーワード:JavaScriptの実行コンテキスト。 – cmbuckley
多くのjQueryのものは完全に無意味です。多くの人が 'this.id'の代わりに'(this).attr( 'id') 'を瞬時に使うのが良いと思っているという事実。私は '$ .proxy'を見ていませんが、全く同じである可能性があります:冗長で無意味です。 –
@Kolink適用する戻り値の関数を返す関数を書くときのことです。ほとんどすべてのライブラリがこの方法を持っています。 – epascarello