【问题标题】:How to set default boolean values in JavaScript?如何在 JavaScript 中设置默认布尔值?
【发布时间】:2013-03-06 01:01:10
【问题描述】:

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

因为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 !!!!!!

对于myCar,它有效,因为undefined || truetrue,但正如您所见,它不适用于myOtherCar,因为false || truetrue。更改顺序无济于事,因为 true || false 仍然是 true

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

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

干杯!

【问题讨论】:

    标签: javascript boolean


    【解决方案1】:

    你可以这样做:

    this.hasWheels = hasWheels !== false;
    

    这会为您提供 true 值,除非 hasWheels 明确为 false。 (其他虚假值,包括nullundefined,将导致true,我认为这是您想要的。)

    【讨论】:

    • @zeMirco:所以您实际上是在要求将除false 之外的所有虚假值视为true?如果是这样,我想知道您为什么要首先使用||。显然,对false 的显式测试将是最简单的解决方案。
    • @thesystem - 他不想使用||;它只是将|| 用于非布尔值的结转。我认为这个问题已发布,因为||没有在做需要做的事情。
    • 如果是这样的话,也许你也可以使用^=来解决这个问题。
    • @minopret - 你会怎么做?
    • 这是一个很酷的技巧,在我的例子中,我希望未定义的参数默认为false,所以我使用了this.hasWheels = hasWheels === true
    【解决方案2】:

    怎么样:

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

    您的另一个选择是:

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

    【讨论】:

    • 这项工作做得很好,尽管我建议使用? !!hasWheels 而不是? hasWheels 来保证truefalse(仅此而已)分配给this.hasWheels .
    【解决方案3】:

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

    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'
    
    • value1 是特别从 value0 为字符串“假”制作的,如果需要它是布尔假的话。我发现这种放松有时很有用。
    • value2value3 是对原始发布答案的修改,以保持一致性,没有改变结果。
    • value4 是 Babel 编译默认参数的方式。

    【讨论】:

      【解决方案4】:

      您可以使用 ECMA6 中的Default function parameters 功能。今天,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
      

      【讨论】:

      • 但如果有人传入虚假值(即空字符串)this.hasWheels 将被分配该空字符串而不是布尔值。可能要加hasWheels = (typeof hasWheels === 'boolean') ? hasWheels : true;
      【解决方案5】:

      没有太多混乱,您可以这样做以获得默认的 true。

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

      获取默认 false

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

      【讨论】:

        猜你喜欢
        • 2019-11-09
        • 2015-03-28
        • 1970-01-01
        • 2020-05-21
        • 1970-01-01
        • 2017-04-17
        • 2022-01-25
        • 2019-04-21
        • 2018-10-14
        相关资源
        最近更新 更多