如何在JavaScript中设置默认布尔值?

如何在JavaScript中设置默认布尔值?,javascript,boolean,Javascript,Boolean,在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' 这是因为color是undef

在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'
这是因为
color
undefined
,而
undefined | | String
始终是
字符串。当然,在
String | | undefined
String
的情况下,这也是另一种情况。当出现两个
字符串时,第一个字符串获胜
“this”| |“that”
就是
“this”
。它不会反过来工作,因为
“that”| |“this”
就是
“that”

问题是:如何使用布尔值实现同样的效果

以下面的例子为例

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 !!!!!!
对于
myCar
而言,它之所以有效是因为
undefined | | true
true
,但正如您所见,它不适用于
myOtherCar
,因为
false | | true
true
。更改顺序没有帮助,因为
true | | false
仍然是
true

因此,我是否遗漏了一些东西,或者以下是设置默认值的唯一方法

this.hasWheels = (hasWheels === false) ? false: true
干杯

那么:

this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;
你的另一个选择是:

this.hasWheels = arguments.length > 0 ? hasWheels : true;
您可以这样做:

this.hasWheels = hasWheels !== false;
这将获得一个
true
值,除非
hasWheels
显式为
false
。(其他falsy值,包括
null
undefined
,将导致
true
,我认为这正是您想要的。)

您可以在ECMA6中使用该功能。如今,ECMA6在浏览器中仍然不完全受支持,但您可以立即使用并开始使用新功能

因此,原始示例将变得非常简单:

// 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

张贴的答案中有一些变化需要注意

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'
var=函数(值){
this.value0=值!==false;
this.value1=value!==false&&value!==false;

this.value2=arguments.length无需太多混淆,您可以这样做以获得默认值true

this.hasWheels=typeof hasWheels===“boolean”?hasWheels:true

获取默认值false


this.hasWheels=typeof hasWheels==='boolean'?false

@zeMirco:那么你实际上是在要求所有的假值,除了
false
被认为是
true
?如果是这样的话,我想知道你为什么首先要使用
|
。显然,
false
的显式测试是最简单的解决方案这个问题被贴出来了,我想,因为他不想使用
|
,它只是对非布尔值使用
|
后的遗留问题。我想,这个问题是因为
|
没有做需要做的事情。如果是这样的话,也许你也可以使用
^=/code>@minopret-你会怎么做?这是一个很酷的问题k、 在我的例子中,我希望未定义的参数被默认为
false
,所以我使用了
this.hasWheels=hasWheels==true
。这做得很好,尽管我建议使用
?!!hasWheels
而不是
?hasWheels
,以保证
true
false
(没有其他内容)分配给
此.hasWheels
。但是如果有人传入falsy值(即空字符串)
此.hasWheels
将分配该空字符串而不是布尔值。可能要添加
hasWheels=(typeof hasWheels=='boolean')?hasWheels:true;