2013-03-17 34 views
41

默认设置在JavaScript中可选值一般通过||字符如何在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 

干杯!

回答

90

你可以这样做:

this.hasWheels = hasWheels !== false; 

,让你一true值除非hasWheels是明确false 。 (其他falsy值,包括nullundefined,将导致true,我认为这是你想要的。)

+0

这很聪明!我喜欢。 – zemirco 2013-03-17 18:05:44

+0

@zeMirco:所以你实际上是要求所有错误的值,除了'false'被认为是'true'?如果是这样,我会想知道为什么你想首先使用'||'。很明显,对“错误”的明确测试将是最简单的解决方案。 – 2013-03-17 18:11:28

+0

@thesystem - 他不想使用'||';它只是将'||'用于非布尔值。我想这个问题已经发布了,因为'||'不是在做需要的东西。 – 2013-03-17 18:13:10

6

如何:

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

你的另一种选择是:

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

这样做很好,虽然我会建议使用'?有hasWheels'而不是'? hasWheels'来保证“this.hasWheels”被赋值为“true”或“false”(没有别的)。 – 2013-03-17 18:38:20

3

可以使用Default function parameters功能在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 
+1

但是如果某人传递了虚假值(即空字符串)'this.hasWheels'将被分配空字符串而不是布尔值。可能想添加'hasWheels =(typeof hasWheels ==='boolean')? hasWheels:true;' – 2016-05-12 11:59:22

2

还有要注意从公布答案的变化:

所以,最初的例子会变得简单。

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做字符串 '假',如果一个人需要它是布尔值false。我偶然发现这种放松有用。
  • value2value3是修改原始发布的答案的一致性,而不会改变结果。
  • value4是巴贝尔如何编译默认参数。