如何在 JavaScript 中设置默认布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15464169/
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
How to set default boolean values in JavaScript?
提问by zemirco
Setting default optional values in JavaScript is usually done via the ||character
在 JavaScript 中设置默认可选值通常是通过||字符完成的
var Car = function(color) {
this.color = color || 'blue';
};
var myCar = new Car();
console.log(myCar.color); // 'blue'
var myOtherCar = new Car('yellow');
console.log(myOtherCar.color); // 'yellow'
That works because coloris undefinedand undefined || Stringis always the String. Of course that also works the other way around String || undefinedis String. When two Stringsare present the first one wins 'this' || 'that'is 'this'. It does NOT work the other way around as 'that' || 'this'is 'that'.
这是有效的,因为color是undefined并且undefined || String始终是String. 当然,这也适用周围的其他方式的String || undefined是String。当两个Strings存在于第一个获胜'this' || 'that'是'this'。这是行不通的四周,另一种方式'that' || 'this'是'that'。
The question is: How can I achieve the same with boolean values?
问题是:如何使用布尔值实现相同的目标?
Take the following example
看下面的例子
var Car = function(hasWheels) {
this.hasWheels = hasWheels || true;
}
var myCar = new Car();
console.log(myCar.hasWheels); // true
var myOtherCar = new Car(false)
console.log(myOtherCar.hasWheels); // ALSO true !!!!!!
For myCarit works because undefined || trueis truebut as you can see it does NOT work for myOtherCarbecause false || trueis true. Changing the order doesn't help as true || falseis still true.
因为myCar它有效,因为undefined || trueistrue但正如你所看到的,它不适用于myOtherCar因为false || trueis true。更改顺序true || false仍然没有帮助true。
Therefore, am I missing something here or is the following the only way to set the default value?
因此,我在这里遗漏了什么还是以下是设置默认值的唯一方法?
this.hasWheels = (hasWheels === false) ? false: true
Cheers!
干杯!
回答by Ted Hopp
You can do this:
你可以这样做:
this.hasWheels = hasWheels !== false;
That gets you a truevalue except when hasWheelsis explicitly false. (Other falsy values, including nulland undefined, will result in true, which I think is what you want.)
这会为您提供一个true值,除非 whenhasWheels是明确的false。(其他虚假值,包括nulland undefined,将导致true,我认为这是你想要的。)
回答by Chris Cooper
How about:
怎么样:
this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;
Your other option is:
你的另一个选择是:
this.hasWheels = arguments.length > 0 ? hasWheels : true;
回答by Season
There are variations to be noted of from posted answers.
发布的答案有一些变化需要注意。
var Var = function( value ) {
this.value0 = value !== false;
this.value1 = value !== false && value !== 'false';
this.value2 = arguments.length <= 0 ? true : arguments[0];
this.value3 = arguments[0] === undefined ? true : arguments[0];
this.value4 = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
};
value0 value1 value2 value3 value4
---------------------------------------------------------------------------
Var("") true true true true true
Var("''") true true '' '' ''
Var("0") true true 0 0 0
Var("'0'") true true '0' '0' '0'
Var("NaN") true true NaN NaN NaN
Var("'NaN'") true true 'NaN' 'NaN' 'NaN'
Var("null") true true null null null
Var("'null'") true true 'null' 'null' 'null'
Var("undefined") true true undefined true true
Var("'undefined'") true true 'undefined' 'undefined' 'undefined'
Var("true") true true true true true
Var("'true'") true true 'true' 'true' 'true'
Var("false") false false false false false
Var("'false'") true false 'false' 'false' 'false'
value1is made especially fromvalue0for string 'false' if one needs it to be boolean false. I found this relaxation useful occationally.value2andvalue3are modifications of original posted answers for consistency, without changed results.value4is how Babel compiles for default parameters.
value1value0如果需要它是布尔假,则特别是从字符串 'false' 制作的。我发现这种放松有时很有用。value2并且value3是对原始发布答案的修改以保持一致性,不更改结果。value4是 Babel 为默认参数编译的方式。
回答by kmandov
You can use the Default function parametersfeature in ECMA6. Today, ECMA6 is still not fully supported in the browser but you can use babeland start using the new features right away.
您可以使用ECMA6 中的默认函数参数功能。今天,浏览器仍然不完全支持 ECMA6,但您可以使用babel并立即开始使用新功能。
So, the original example will become as simple as:
因此,原始示例将变得如此简单:
// specify default value for the hasWheels parameter
var Car = function(hasWheels = true) {
this.hasWheels = hasWheels;
}
var myCar = new Car();
console.log(myCar.hasWheels); // true
var myOtherCar = new Car(false)
console.log(myOtherCar.hasWheels); // false
回答by Nithin Chandran
What I did was
我所做的是
this.hasWheels=typeof hasWheels === 'boolean'?hasWheels:true
this.hasWheels=typeof hasWheels === 'boolean'?hasWheels:true

