javascript 关于Javascript函数参数的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6446304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question on Javascript Function Parameters
提问by simplified.
I was trying to write some javascript function and realised that
我试图编写一些 javascript 函数并意识到
function testFunction(input_1, input_2, input_3) {
alert("alert");
}
however when i call the function like this:
但是,当我像这样调用函数时:
<input type="button" value="click" onclick="testFunction("1", "2")">
why will it still work even with just two parameters?
为什么即使只有两个参数它仍然可以工作?
回答by SLaks
You can call a Javascript function with any number of parameters, regardless of the function's definition.
无论函数的定义如何,您都可以使用任意数量的参数调用 Javascript 函数。
Any named parameters that weren't passed will be undefined
.
任何未传递的命名参数都将是undefined
.
Extra parameters can be accessed through the arguments
array-like object.
可以通过arguments
类似数组的对象访问额外的参数。
回答by gion_13
It doesn't actually matter how many parameters you are providing. the function interprets them and creates the arguments
object (which acts as an array of parameters).
Here's an example:
您提供多少参数实际上并不重要。该函数解释它们并创建arguments
对象(充当参数数组)。下面是一个例子:
function sum(){
if(arguments.length === 0)
return 0;
if(arguments.length === 1)
return arguments[0];
return arguments[0] + sum.apply(this, [].slice.call(arguments, 1));
}
It's not the most efficient solution, but it provides a short peak at how functions can handle arguments.
这不是最有效的解决方案,但它为函数如何处理参数提供了一个短暂的高峰。
回答by Charlie Martin
Because javascript treats your parameters as an array; if you never go beyond the second item, it never notices an argument is missing.
因为 javascript 将您的参数视为数组;如果你永远不会超出第二项,它永远不会注意到缺少一个参数。
回答by Barot Haresh
Javascript does not support Method overloading hence method will be execute in order of their occurence irrelevant of arguments passed Because javascript has no type checking on arguments or required qty of arguments, you can just have one implementation of testFunction() that can adapt to what arguments were passed to it by checking the type, presence or quantity of arguments..
Javascript 不支持方法重载,因此方法将按照它们出现的顺序执行,与传递的参数无关 因为 javascript 没有对参数进行类型检查或需要的参数数量,您只能拥有一个 testFunction() 实现来适应哪些参数通过检查参数的类型、存在或数量传递给它..
回答by Barot Haresh
Try using the below code
尝试使用以下代码
var CVH = {
createFunction: function (validationFunction, extParamData, extParamData1) {
var originalFunction = validationFunction;
var extParam = extParamData;
var extParam1 = extParamData1;
return function (src, args) {
// Proxy the call...
return originalFunction(src, args, extParam, extParam1);
}
}
}
function testFunction(input_1, input_2, input_3) {
alert("alert");
}
and you can call this function as below
你可以调用这个函数如下
<input type="button" value="click" onclick="CVH.createFunction(testFunction('1', '2'),'3','4')">
回答by Ahmad
the third parameter can be optional and will have a null default value. If you explicitly want to require a parameter then that is a different story
第三个参数可以是可选的,默认值为空。如果你明确想要一个参数,那就是另一回事了
回答by Brandon Boone
Because Parameters are optional
因为参数是可选的
Some Reading: http://www.tipstrs.com/tip/354/Using-optional-parameters-in-Javascript-functions
一些阅读:http: //www.tipstrs.com/tip/354/Using-optional-parameters-in-Javascript-functions
回答by mtazva
Javascript is a very dynamic language and will assume a value of "undefined" for any parameters not passed a value.
Javascript 是一种非常动态的语言,对于未传递值的任何参数,它都会假定值为“未定义”。