76

我想知道,你能创建一个带有可选参数的函数吗?

例子:

function parameterTest(test)
{
   if exists(test)
   {
     alert('the parameter exists...');
   }
   else
   {
     alert('The parameter doesn\'t exist...');
   }
}

因此,如果您调用parameterTest(),结果将是一条消息“参数不存在...”。如果你打电话parameterTest(true),那么它会返回“参数存在......”。

这可能吗?

4

9 回答 9

109

这是一个非常常见的模式。

您可以使用

function parameterTest(bool) {
  if (bool !== undefined) {

然后,您可以使用其中一种形式调用您的函数:

 parameterTest();
 parameterTest(someValue);

注意不要犯测试的频繁错误

if (!bool) {

因为您无法将未提供的值与false,0或区分开来""

于 2012-10-22T20:53:09.463 回答
13
function parameterTest(bool)
{
   if(typeof bool !== 'undefined')
   {
     alert('the parameter exists...');
   }
   else
   {
     alert('The parameter doesn\'t exist...');
   }
}
于 2012-10-22T20:53:21.010 回答
7

在 JavaScript 中,如果您忽略提供参数,它将默认为undefined.

您可以在浏览器控制台或使用JSFiddle中轻松地自己尝试一下。

正如您所说,您可以检查参数是否存在,然后编写一个可以使用参数的函数。但是,JavaScript Garden(一个很好的资源)建议在大多数其他情况下远离typeof,因为它的输出几乎没有用(查看typeof 的结果表)。

于 2012-10-22T20:51:46.000 回答
3
function parameterTest(p) {
    if ( p === undefined)
        alert('The parameter doesn\'t exist...');
    else
        alert('the parameter exists...');
}
于 2012-10-22T20:55:24.127 回答
3

最好的检查方法:如果参数不是未定义的

function parameterTest(param) {
    if (param !== undefined)
    ...

参数也可以是变量或函数名

于 2014-01-16T13:43:56.457 回答
2

如果不存在,则初始化默认值:

function loadDialog(fn, f, local, anim) {
  switch (arguments.length) {  
    case 1: f=null;
    case 2: local=false;
    case 3: anim=false;
  }
  ...
}
于 2020-07-12T08:16:17.193 回答
2

这些答案都不是为我做的。我需要知道是否有人使用过该参数。如果有人调用传递的函数,undefined我想将其视为他们使用该值作为参数。无论如何,使用一些现代 JS 的解决方案非常简单:

function parameterTest(...test) {
  if (test.length) {
    return `exists`;
  } else {
    return `not exists`;
  }
}

parameterTest(123);         // exists
parameterTest(undefined);   // exists
parameterTest();            // not exists
parameterTest(window.blah); // exists

对于较旧的浏览器,您可以使用参数:

function parameterTest() {
  if (arguments.length) {
    return "exists";
  } else {
    return "not exists";
  }
}
于 2021-06-04T09:14:47.950 回答
1

null == undefined是真的

if (arg == null){
    // arg was not passed.
}

代码示例:

var button = document.querySelector("button");

function myFunction(arg){
  if(arg == null){
    alert("argument was not passed.");
  } else {
    alert("argument " + arg + " was passed.");
  }
}
<button onclick="myFunction('foo');">click to fire function w arg</button>
<br><br>
<button onclick="myFunction();">click to fire function w/o arg</button>

于 2018-08-15T20:31:58.163 回答
-2

我知道这是旧的,但这是我检查的首选方法,并为函数分配默认值:

function testParamFunction(param1, param2) {
    param1 = typeof param1 === 'undefined' ? null : param1;
    param2 = typeof param2 === 'undefined' ? 'default' : param2;

    // exit if the required parameter is not passed
    if (param1 === null) {
        console.error('Required parameter was not passed');
        return;
    }

    // param2 is not mandatory and is assigned a default value so 
    // things continue as long as param1 has a value
}
于 2016-04-12T00:43:47.407 回答