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

How to set default boolean values in JavaScript?

本文关键字:设置 默认 布尔值 JavaScript      更新时间:2023-09-26

在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'

这是因为colorundefined,而undefined || String始终是String。当然,围绕String || undefined的另一种方式也是String。当存在两个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 !!!!!!

对于myCar,它起作用是因为undefined || truetrue,但正如您所看到的,它不适用于myOtherCar,因为false || truetrue。更改顺序没有帮助,因为true || false仍然是true

因此,我是遗漏了什么,还是以下是设置默认值的唯一方法?

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

干杯!

您可以这样做:

this.hasWheels = hasWheels !== false;

这将为您获得一个true值,除非hasWheels显式为false。(其他错误的值,包括nullundefined,将导致true,我认为这正是您想要的。(

怎么样:

this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;

你的另一个选择是:

this.hasWheels = arguments.length > 0 ? hasWheels : true;

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

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'
  • 如果需要字符串"false"为布尔值false,则value1特别是从value0生成的。我发现这种放松有时很有用
  • value2value3是为了保持一致性而对原始发布的答案进行的修改,没有更改结果
  • value4是Babel为默认参数编译的方式

您可以使用ECMA6中的默认函数参数功能。如今,浏览器中仍然不完全支持ECMA6,但您可以使用babel并立即开始使用新功能。

因此,最初的例子将变得如此简单:

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

不用太多困惑,您可以这样做来获得默认值true。

this.hasWheels=typeof hasWheels === 'boolean'?hasWheels:true

获取默认的错误

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