我需要将空参数传递给 javascript 函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12556229/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:42:02  来源:igfitidea点击:

Do I need to pass empty parameters to a javascript function?

javascriptfunctionparametersoptional-parameters

提问by frequent

Say I have a function like this, which i'm calling throughout a script:

假设我有一个这样的函数,我在整个脚本中调用它:

  function form_senden( form_name, cnf, confirm_txt, trigger_field, ,do_check, chknfcs, allow, errorMsg ){
  // do something 
  }

On most of my function calls, I'm only passing the first parameter.

在我的大多数函数调用中,我只传递第一个参数。

Question:
Is it ok in this case to omit passing empty parameters like so:

问题:
在这种情况下可以省略传递空参数,如下所示:

  form_senden("abc");

Or do I need to pass all parameters regardless if they are used like so:

或者我是否需要传递所有参数,无论它们是否像这样使用:

  form_senden("abc","","","","","","","","");

Thanks!

谢谢!

采纳答案by camino

form_senden("abc"); is ok

form_senden("abc"); 没问题

the other parameters will be initialized as undefined

其他参数将被初始化为 undefined

回答by Gung Foo

It is okay to only pass the first parameter as all other will not be set. If you want to set the 1st and 3rd argument, you will need to make the 2nd null, like so:

可以只传递第一个参数,因为不会设置所有其他参数。如果要设置第 1 个和第 3 个参数,则需要将第 2 个参数设为 null,如下所示:

form_senden("a",null,"b");

回答by Teena Thomas

you may do just form_senden("abc");putting default values in the function definition.

您可以只form_senden("abc");在函数定义中放置默认值。

    function form_senden( form_name, cnf , confirm_txt , trigger_field , ,do_check , chknfcs, allow, errorMsg  ){

if(typeof(cnf)==='undefined') cnf = '';
if(typeof(confirm_txt)==='undefined') confirm_txt = ''; ...and so on
// do something 
}

回答by lanzz

Omitting function parameters is okay, the missing parameters will have undefinedvalue in the function.

省略函数参数是可以的,缺少的参数将undefined在函数中有价值。

回答by Smit Luvani

Here, I found a solution to pass null or empty values to the function. It works in node.js

在这里,我找到了将 null 或空值传递给函数的解决方案。它适用于 node.js

    function returnJSON(name,value1,value2,text){
        return object = {
            'name':name,
            'value1':value1,
            'value2':value2,
            'text':text
        }
    }
    returnJSON('Hello World',undefined,undefined,'This is Text');

回答by mangonights

for completeness, in ES6 (ES2015) and later, default values can be specified

为了完整起见,在 ES6 (ES2015) 及更高版本中,可以指定默认值

// `b` will default to 1

function multiply(a, b = 1) {
  return a * b;
}

回答by jamess

Regarding default parameter settings. If you are using a default parameter setting such as function f(arg=1) {}, the setting of the default value will not work with false, nulll, or zero passed in. You must leave the arg/parameter empty or pass in undefined.

关于默认参数设置。如果您使用的是默认参数设置,例如 function f(arg=1) {},则默认值的设置不适用于传入的 false、nulll 或零。您必须将 arg/参数留空或传入不明确的。

回答by Mauricio Avenda?o

if you have something like:

如果你有类似的东西:

const myFunc = (a = 1, b = 2, c = 3) => a + b + c;

const myFunc = (a = 1, b = 2, c = 3) => a + b + c;

if you call myFunc();you will get 6.

如果你打电话,myFunc();你会得到6

but if you for example wants to call myFunc with c = 4and let aand bat its default values, you can call myFunclike this:

但是,例如,如果您想使用c = 4调用 myFunc并让ab为其默认值,则可以像这样调用myFunc

myFunc(undefined, undefined, 4);and this will return 7.

myFunc(undefined, undefined, 4);这将返回7