admin管理员组文章数量:1415137
In jQuery API doc, the jQuery.proxy function usage:
jQuery.proxy( function, context )
function The function whose context will be changed.
context The object to which the context (this) of the function should be set.
jQuery.proxy( context, name )
context The object to which the context of the function should be set.
name The name of the function whose context will be changed (should be a property of the context object).
proxy : function(fn, proxy, thisObject){
if ( arguments.length === 2 ) {
if (typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[proxy];
proxy = undefined;
} else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// So proxy can be declared as an argument
return proxy;
}
But when I look into jQuery source code, of function proxy. I found there're 3 parameters declared.
So I wonder what's the use of third param, cannot understand the code :(
I write a code segment to test the function.
var someObj = { somefnc : function() {} };
function fnc() {
this.somefnc();
console.log(arguments);
}
var proxyedFnc = jQuery.proxy(fnc, undefined, someObj, "arg1","arg2");
proxyedFnc();
//output: []
And I wonder why the arguments were not passed to fnc..
In jQuery API doc, the jQuery.proxy function usage:
jQuery.proxy( function, context )
function The function whose context will be changed.
context The object to which the context (this) of the function should be set.
jQuery.proxy( context, name )
context The object to which the context of the function should be set.
name The name of the function whose context will be changed (should be a property of the context object).
proxy : function(fn, proxy, thisObject){
if ( arguments.length === 2 ) {
if (typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[proxy];
proxy = undefined;
} else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// So proxy can be declared as an argument
return proxy;
}
But when I look into jQuery source code, of function proxy. I found there're 3 parameters declared.
So I wonder what's the use of third param, cannot understand the code :(
I write a code segment to test the function.
var someObj = { somefnc : function() {} };
function fnc() {
this.somefnc();
console.log(arguments);
}
var proxyedFnc = jQuery.proxy(fnc, undefined, someObj, "arg1","arg2");
proxyedFnc();
//output: []
And I wonder why the arguments were not passed to fnc..
Share Improve this question asked Mar 22, 2012 at 7:10 ThemeZThemeZ 4931 gold badge5 silver badges15 bronze badges2 Answers
Reset to default 6xdazz is right, the latest 1.7.2 version has a different syntax, that also allows multiple extra arguments to be concated into the apply
and passed into the proxy function, f.ex:
var fn = function() {
console.log(this.foo, arguments);
};
var obj = {
foo: 'bar'
};
$.proxy(fn, obj, 'one', 'two')();
Running this code will print bar ["one", "two"]
You would get the same result by doing:
$.proxy(fn, obj)('one', 'two');
I might also add that none of this is documented in the official API, so things might work differently "under the hood" in different versions. This code is tested in 1.7.2.
Below is the source es from jquery-1.7.2.js, Are you sure you check the same version between source and api doc?
// Bind a function to a context, optionally partially applying any
// arguments.
proxy: function( fn, context ) {
if ( typeof context === "string" ) {
var 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
var args = slice.call( arguments, 2 ),
proxy = function() {
return fn.apply( context, args.concat( 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 || proxy.guid || jQuery.guid++;
return proxy;
},
本文标签: javascriptWhat is the 3rd param in jQueryproxy used forStack Overflow
版权声明:本文标题:javascript - What is the 3rd param in jQuery.proxy used for? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745206635a2647669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论