javascript 在javascript中将默认值设置为未定义的变量

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

setting a default value to an undefined variable in javascript

javascriptundefineddefault-value

提问by Jimmery

Ideally I want to be able to write something like:

理想情况下,我希望能够编写如下内容:

function a( b ) {
    b.defaultVal( 1 );
    return b;
}

The intention of this is that if bis any defined value, bwill remain as that value; but if bis undefined, then bwill be set to the value specified in the parameter of defaultVal(), in this case 1.

这样做的目的是,如果b是任何定义的值,b将保持为该值;但如果b未定义,b则将设置为 的参数中指定的值defaultVal(),在本例中为1

Is this even possible?

这甚至可能吗?

Ive been toying about with something like this:

我一直在玩这样的事情:

String.prototype.defaultVal=function(valOnUndefined){
    if(typeof this==='undefined'){
        return valOnUndefined;
    }else{
        return this;
    }
};

But I'm not having any success applying this kind of logic to anyvariable, particularly undefined variables.

但是我没有成功将这种逻辑应用于任何变量,特别是未定义的变量。

Does anyone have any thoughts on this? Can it be done? Or am I barking up the wrong tree?

有没有人对此有任何想法?可以做到吗?还是我叫错了树?

回答by Elias Van Ootegem

Why not use the default operator:

为什么不使用默认运算符:

function someF(b)
{
    b = b || 1;
    return b;
}

Job done! Here's more infoabout how it works.

任务完成!这是有关其工作原理的更多信息

Just as a side-note: your prototype won't work, because you're augmenting the Stringprototype, whereas if your variable is undefined, the Stringprototype doesn't exactly apply.

顺便说一句:您的原型将不起作用,因为您正在扩充String原型,而如果您的变量未定义,则String原型并不完全适用。

To be absolutely, super-duper-sure that you're only switching to the default value, when breally is undefined, you could do this:

绝对的,超级骗子确保您只切换到默认值,当b确实未定义时,您可以这样做:

var someF = (function(undefined)//get the real undefined value
{
    return function(b)
    {
        b = b === undefined ? 1 : b;
        return b;
    }
})(); // by not passing any arguments

回答by BLSully

Well the most important thing to learn here is if a variable is undefinedyou, by default, do not have any methods available on it. So you need to use a method that can operate on the variable, but is not a member/method ofthat variable.

那么在这里学习的最重要的事情是如果一个变量是undefined你,默认情况下,没有任何可用的方法。所以,你需要使用可以对变量进行操作,而不是其成员/方法的方法变量。

Try something like:

尝试类似:

function defaultValue(myVar, defaultVal){
    if(typeof myVar === "undefined") myVar = defaultVal;
    return myVar;
}

What you're trying to do is the equivalent of this:

你想要做的是相当于这个:

undefined.prototype.defaultValue = function(val) { }

...Which for obvious reasons, doesn't work.

...出于明显的原因,这不起作用。

回答by Noel

While the two answers already provided are correct, the language has progressed since then. If your target browsers supportit, you can use default function parameterslike this:

虽然已经提供的两个答案是正确的,但从那时起语言已经取得了进步。如果您的目标浏览器支持它,您可以使用这样的默认函数参数

function greetMe(greeting = "Hello", name = "World") {
  console.log(greeting + ", " + name + "!");
}

greetMe(); // prints "Hello, World!"
greetMe("Howdy", "Tiger"); // prints "Howdy, Tiger!"

Hereare the MDN docs on default function parameters.

以下是有关默认函数参数的 MDN 文档。