[英]pass parents function arguments in a nested function?
I am having a problem with arguments object in a nested function, seems like arguments.length is taken from parent function while arguments[0] is taken from nested function... anybody can explain why this is happening?and show me the most effective將父 foo 的 arguments 傳遞給 bar 的方法?
$.fn.foo = function(color1, color2, time ){
return this.each(function bar(){
for(var i = 0;i < (arguments.length - 1);i++){
alert(arguments.length); //this is taken from foo function and returns 2
alert(arguments[i]); //this is taken from bar
}
});
};
arguments
將始終(除非更改)具有當前執行的 function 的 scope ,在您的情況下是bar
。
閱讀 jquery .each
文檔, function“原型”如下:
.each( function(index, Element) )
So of course arguments.length
will return 2. Whether or not you have named variables to catch those 2 sent arguments is another story, but the arguments object will have length 2 if the function was called with 2 arguments.
簡單的解決方案:獲取對arguments
的本地引用。
$.fn.foo = function(color1, color2, time ){
var args = arguments; // Create a private reference
return this.each(function bar(){
alert(args.length); //Use private reference
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.