Javascript 如何将值“未定义”传递给具有多个参数的函数?

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

How to pass the value 'undefined' to a function with multiple parameters?

javascriptfunctionparameter-passingundefined

提问by ajax333221

I want to pass the value of 'undefined' on a multiple parameter function but without omitting the parameter.

我想在多参数函数上传递 'undefined' 的值,但不省略参数。

What do I mean with "without omitting the parameter". I mean that we should not just omit the parm2like this example:

“不省略参数”是什么意思。我的意思是我们不应该只是省略parm2像这个例子:

function myFunction (parm1, parm2) {}
myFunction("abc");

This will indeed make parm2undefined, but I am not allowed to do it this way because I will need to specify other parameters AFTER the omitted parameter, so the previous method won't work in the case I want to make parm1undefined BUT also want to have other parameters after this one to hold a value.

这确实会使parm2undefined,但我不允许这样做,因为我需要在省略的参数之后指定其他参数,所以在我想要parm1undefined 但也想有的情况下,以前的方法将不起作用在此之后的其他参数保存一个值。

I have tried solving the problem with:

我试过用以下方法解决问题:

myFunction( ,"abc"); //doesn't seem to work


Update:

更新:

and myFunction(undefined, "abc");? this reliably works now.

myFunction(undefined, "abc");?这现在可靠地工作。

However, it is worth mentioning that:

不过,值得一提的是:

Setting a variable to undefinedis considered a bad practice, we should be using nullinstead.

将变量设置为undefined被认为是一种不好的做法,我们应该null改用它。

回答by xdazz

myFunction(undefined,"abc");this way should work, what is the problem?

myFunction(undefined,"abc");这种方式应该有效,有什么问题?

see here

这里

Here is undefined documentationfrom mozilla, supported by all browsers

这是来自 mozilla 的未定义文档,所有浏览器都支持

回答by bkbooth

The voidoperatorseems to be the most common way to explicitly get undefined.

void操作似乎是最常见的方式明确地得到undefined

You would use it like this in your example:

您可以在示例中像这样使用它:

myFunction(void 0, "abc");

It's also a reliable method for comparing against undefinedthat is guarded against undefinedbeing accidentally overridden in older JavaScript environments:

它也是一种可靠的比较方法undefined,可以防止undefined在较旧的 JavaScript 环境中被意外覆盖:

var x;
if (x === void 0) {
    // this will execute
}

回答by dbrin

A better approach might be passing Objectwith named attributes and then look for those specific attribute values. This way you don't have to be dependent on number of arguments.
Example: Object.dummy

更好的方法可能是传递Object命名属性,然后查找那些特定的属性值。这样您就不必依赖于参数的数量。
例子:Object.dummy

回答by ajax333221

I just had an idea and it seems to work:

我只是有一个想法,它似乎工作:

var undf;

myFunction(undf, "abc");

I am sure there are better ways, however I post this

我确信有更好的方法,但是我发布了这个

回答by Nipun Madan

You need to handle the function call with undefined variable in the function code itself in one of the following way :-

您需要通过以下方式之一在函数代码本身中处理带有未定义变量的函数调用:-

 function myFunction (parm1, parm2) {

         if (parm1 !== undefined) {
             // execute code if 1st param is undefined
        }

         if (parm2 !== undefined) {
             // execute code if 2 param is undefined
        }

        // execute other part of code

    }

Now you can call the above function in following ways:-

现在您可以通过以下方式调用上述函数:-

myFunction(undefined,"abc"); // with 1st param value not known

myFunction("cde",undefined); // with 2nd param value not known

myFunction("cde","abc");  // with both param value known

回答by Edward Olamisan

You can use applyand an array of parameters to pass "undefined" as one of the parameters. For example, you wanted to pass parm1 as "undefined":

您可以使用apply和参数数组将“未定义”作为参数之一传递。例如,您想将 parm1 传递为“未定义”:

function myFunction (parm1, parm2) {

    if(typeof (parm1) === "undefined"){
        alert("parm1 is undefined")
    }

    if(typeof (parm2) === "undefined"){
        alert("parm2 is undefined")
    }

}

var myParameters = [undefined, "abc"];

myFunction.apply(valueForThis, myParameters );

回答by Niet the Dark Absol

I think the closest you'll get to this is passing nullas a parameter. It's not undefined, but for most cases it's close enough.

我认为你最接近的是null作为参数传递。不是undefined,但在大多数情况下,它已经足够接近了。

回答by Aram Kocharyan

Try to use this method if you plan on adding an indefinite amount of parameters:

如果您打算添加无限数量的参数,请尝试使用此方法:

function myFunc(params) {
    // Define default values
    var name = 'John';
    var age = '40';
    // You can loop through them
    for (var p in params) {
        alert(p + ':' + params[p]);
    }
    // And read them in like this
    if (typeof params.name != 'undefined') {
        name = params.name;
    }
    if (typeof params.age != 'undefined') {
        age = params.age;
    }
    alert(name + ' ' + age);
}

alert('test1');
myFunc({name:'Bob', age:'30'});
alert('test2');
myFunc({name:'Bob'});

回答by davidchambers

myFunction(undefined,"abc")should absolutely work, unless someone cruel has redefinedundefined! If you want to be safe, there are dozens of ways to get the undefined value which avoid the assumption that the thing called "undefined" does in fact have the special undefined value:

myFunction(undefined,"abc")应该绝对有效,除非有人残忍地重新定义了undefined!如果你想安全,有很多方法可以得到 undefined 值,以避免假设被称为“undefined”的东西实际上具有特殊的 undefined 值:

void 0
var undef; undef
[][0]
{}.foo
// `undef` is guaranteed to have the undefined value within this closure
(function(undef){ undef }())
// this expression evaluates as undefined
(function(){}())

void 0is the most widely used (compiled CoffeeScript includes this wherever the undefined value is required).

void 0是最广泛使用的(编译后的 CoffeeScript 在需要未定义值的地方包含它)。

回答by Tomy Varghese

If its possible , could you rearrange the function as myFunction (parm2, parm1)

如果可能,您能否将函数重新排列为 myFunction (parm2, parm1)

ie. try to ensure possible undefined parameters are last in the function , this might not work if more parameters are introduced.

IE。尽量确保可能的未定义参数在函数的最后,如果引入更多参数,这可能不起作用。