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

zem*_*rco 57 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'
Run Code Online (Sandbox Code Playgroud)

这工作,因为colorundefinedundefined || String永远的String.当然,这也适用周围的其他方式的String || undefinedString.当两个Strings人出现时,第一个获胜者'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 !!!!!!
Run Code Online (Sandbox Code Playgroud)

对于myCar它的作品,因为undefined || truetrue,但你可以看到它不工作了myOtherCar,因为false || truetrue.更改订单不帮助为true || false仍然是true.

因此,我在这里遗漏了什么或者以下是设置默认值的唯一方法吗?

this.hasWheels = (hasWheels === false) ? false: true
Run Code Online (Sandbox Code Playgroud)

干杯!

Ted*_*opp 118

你可以这样做:

this.hasWheels = hasWheels !== false;
Run Code Online (Sandbox Code Playgroud)

true除非hasWheels明确表示,否则会获得一个值false.(其他有价值的值,包括nullundefined将导致true,我认为这就是你想要的.)

  • 这是一个很酷的技巧,在我的情况下,我希望未定义的参数默认为"false",所以我使用`this.hasWheels = hasWheels === true`. (7认同)
  • @thesystem - 他不想使用`||`;它只是将 `||` 用于非布尔值的结转。我认为这个问题是发布的,因为`||` _not_ 做需要的事情。 (3认同)

Chr*_*per 6

怎么样:

this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;
Run Code Online (Sandbox Code Playgroud)

你的另一个选择是:

this.hasWheels = arguments.length > 0 ? hasWheels : true;
Run Code Online (Sandbox Code Playgroud)

  • 虽然我建议使用`?!! hasWheels`而不是`?hasWheels`保证将`true`或`false`(以及其他任何内容)分配给`this.hasWheels`. (2认同)

Sea*_*son 5

从发布的答案中需要注意一些变化。

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'
Run Code Online (Sandbox Code Playgroud)
  • value1value0如果需要它是布尔假,则特别是从字符串 'false' 制作的。我发现这种放松有时很有用。
  • value2并且value3是对原始发布答案的修改以保持一致性,不更改结果。
  • value4 是 Babel 为默认参数编译的方式。