イベントハンドラ(つまり、$(...).on('something', myObject.handleSomething)
)としてメソッドを使用するコンテキストで。私は$ .proxyと_.bind(http://jsperf.com/bind-vs-jquery-proxy/27)のパフォーマンスの差が比較的大きいことを発見し、その実装を見ました。jQuery.proxyとアンダースコア.bind
args = core_slice.call(arguments, 2);
proxy = function() {
return fn.apply(context || this, args.concat(core_slice.call(arguments)));
};
をアンダースコア(http://underscorejs.org/docs/underscore.html#section-60が)(ctorのはvar ctor = function(){};
で)戻って終わるながら:
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
ctor.prototype = null;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return self;
};
私は_.bind
は私が引数をバインドすることができますことを理解し
jQueryの(http://james.padolsey.com/jquery/#v=1.10.2&fn=proxyは)戻って終わりますもしmyObject.handleSomething
をイベントハンドラとして使用したいのであれば、new
コールの場合は実用上有利でしょうか?
_.bindAll
と似たようなものを$.proxy
と書くことはできますか?例えば。
$.proxyAll = function (obj) {
for (var attr in obj) if (obj.hasOwnProperty(attr) && $.isFunction(obj[attr])) {
obj[attr] = $.proxy(obj[attr], obj);
}
return obj;
};
あなたのイベントをハンドラにバインドしようとしているので、あなたは 'myObject.handleSomething'によってイベントが処理されるとコンテキストの問題が発生しているようですね。もしそうなら、あなたのイベントデータの一部としてコンテキストを渡すのはなぜですか? –
私はあなたを誤解しているかもしれませんが、それは意味をなさない(イベントデータに 'myObject'コンテキストを取得する方法は?)。私が必要とするのは 'Object.bind()'ですが、IEのためにライブラリを使う必要があります。 underscore.jsとjQueryのパフォーマンスの差はかなり大きく、jQueryの方がはるかに高速です。私の質問の最初の部分は、この速度の違いが一般性の欠如によるものかどうかです。私の質問の2番目の部分は '_.bindAll()'が 'jQuery.proxy()'から直接実装可能かどうかです。 – thebjorn