因为我可以通过调用它的Function.length
属性来确定函数所期望的参数个数,所以有没有办法让我以编程方式创建在运行时插入该函数的正确数量的参数?示例:
var xyz = function(a,b) {};
var bcd = function(a,b,c,d,e,f) { }; // vararg example
var doc = document, func_length = xyz.length;
doc.xyz = (function() {
return function(a,b,c,d,e) { /* works for one but not the other */ } }).call(doc);
/* would prefer to `return function(a,b)` with only 2 parameters, if it is
used for function `xyz` (though returning 5 works fine in this case), and to
`return function(a,b,c,d,e,f)` with 6 if used for function `bcd` (which does
not work with only five params). */
// thinking about xyz.apply(null,arguments)...but which arguments..? :(
// Returning function(a,b,c,d,e) does not support functions with more than five
// parameters...which would mostly be varargs - hence my question
// I am also well aware that I can use an object or an array instead of
// using many params.
/* This is for incorporating a user-defined function for use in my code, while
* allowing for my function to do 'other stuff' afterward. (And allowing for
* varargs, of course).
* Because coding something like: doc.xyz = xyz is inflexible */
如您所见,我不知道该怎么做,或者甚至可能。搜索栏没有给我任何其他类似的问题,否则我不会问......
答案 0 :(得分:2)
NOTE: This answer is a product of misunderstanding but
may help the future visitors of this site.
你真的需要添加参数吗?以这种方式编写函数就足够了:
function foo(){ //No arguments predefined
a = arguments[0] || ""; //first argument or (if not defined) empty string
b = arguments[1] || ""; //second argument etc.
c = arguments[2] || ""; //third argument etc.
alert(a+b+c);
}
foo("Hello ", "world!");
这会提醒“Hello world”。
这就是你所要求的,但它并不像以前的解决方案那么简单 您可以使用随时间变化的所有参数和处理函数定义 元函数。
(function(){ //Wrapper
var foo_meta = function(a,b,c,d){ //Local meta of foo
alert(a+b+c+d); //Do the code
};
window.foo = function(a,b){ //Global foo
return foo_meta(a,b,"","");
};
window.redefine_foo = function(){ //Global foo-changer
//Rewrites foo
window.foo = function(a,b,c){
return foo_meta(a,b,c,"");
};
};
})(); //Wrapper
//Do some code
foo("a","b");
redefine_foo(); //Rewrite foo
foo("a","b","c");
//Note that foo_meta is not defined here
foo_meta == undefined; //It's safe in the wrapper :)
这将警告“ab”然后“abc”。有关包装函数的含义,请参阅参考资料。
参数数组:http://goo.gl/FaLM1H
包装代码:http://goo.gl/uQ5sd0
答案 1 :(得分:0)
如果您向function doWork(a,b,c,d,e),a=7 and b=6
发送两个参数6和7将自动设置,其余参数将被忽略。
答案 2 :(得分:0)
var doc = document;
var xyz = function(a,b) {};
var bcd = function(a,b,c,d,e,f) {};
var obj = {};
// Now, here it is (mostly (sort of)):
obj.userFunc = function(args) {
var args_Array = [];
for (var i=0;i < arguments.length; i++ ) {
args_Array.push(arguments[i])
}
xyz.apply(null,args_Array); // or 'this'. or 'undefined'. Whatever you want.
// do other stuff
return this; // we know what to do to make 'this' scope explicit
} // end
var thisFunc = 'xyz'
doc[thisFunc] = obj.userFunc;
doc.xyz('h','i');
doc.xyz('h','i','j');
doc.xyz('h','i','j','k');
doc.xyz('h','i').xyz('j','l').xyz('j','q'); // etc.
诀窍是使用arguments
对象,它方便地将所有参数同化为一个随时可用的对象,将每个值推入一个数组然后apply
该函数。
如果您想知道大惊小怪的事情,我想完全将用户定义的功能合并到另一个功能中,同时仍然可以做其他事情&#39;之后。我之前的例子在一定程度上起了作用,但没有支持varargs。这样做。
这种方法比doc[thisFunc] = userDefinedFunction
:) 4/26/2014
答案 3 :(得分:0)
这是一个连接参数的示例,无论参数的数量如何,以显示如何将函数参数转换为数组,然后像处理任何其他数组一样进行处理。
function foo(){ //No arguments predefined
// convert to real array
var args = Array.prototype.slice.call(arguments);
// or if Array generics are available
var args = Array.slice(arguments);
console.log(args.join(' '));
}
foo('Hello', 'world!');
foo('Hello', 'wonderful', 'world!');
以下是fiddle
答案 4 :(得分:0)
为什么不将一个对象传递给函数并使用JQuery扩展。
e.g。
var parm =
{ x: 1, y : 2};
f(p) {
p = $_.extend({...defaults here}, p);
...
}