javascript - How does jquery proxy work -
i more curious else. how pass context function. wrap function in object? sure there simple straightforward code doing in js without jquery proxy
function abc(){ console.log(this.name); } var obj={name:"something"}; $.proxy(abc,obj);
how can without jquery proxy?
without jquery may use bind :
var newfunction = abc.bind(obj);
and if want compatible ie8, may do
var newfunction = function(){ abc.call(obj) };
here's how jquery :
// bind function context, optionally partially applying // arguments. proxy: function( fn, context ) { var args, proxy, tmp; if ( typeof context === "string" ) { tmp = fn[ context ]; context = fn; fn = tmp; } // quick check determine if target callable, in spec // throws typeerror, 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 guid of unique handler same of original handler, can removed proxy.guid = fn.guid = fn.guid || jquery.guid++; return proxy; },
Comments
Post a Comment