如何在JavaScript中设置默认布尔值
How to set default boolean values in 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'
这是因为color
是undefined
,而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 || true
是true
,但正如您所看到的,它不适用于myOtherCar
,因为false || true
是true
。更改顺序没有帮助,因为true || false
仍然是true
。
因此,我是遗漏了什么,还是以下是设置默认值的唯一方法?
this.hasWheels = (hasWheels === false) ? false: true
干杯!
您可以这样做:
this.hasWheels = hasWheels !== false;
这将为您获得一个true
值,除非hasWheels
显式为false
。(其他错误的值,包括null
和undefined
,将导致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
生成的。我发现这种放松有时很有用 value2
和value3
是为了保持一致性而对原始发布的答案进行的修改,没有更改结果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
- 如何设置默认<ui视图>在另一<ui视图>
- jQuery Datepicker从日期开始设置默认值
- 量角器-如何在量角器中设置默认的打字语言
- 在KeystoneJS中为Types.Money设置默认值
- 在AngularJS Dropdownlost中设置默认文本选项
- 如何为显示jquery滑块值的文本框设置默认值
- Angular:如何使用ngRepeat并设置默认单选按钮选项
- 在Bootstrap下拉菜单中设置默认值
- 如何为 jquery 自动完成框设置默认值
- 如何在 ag-grid 中设置默认的最小和最大列宽
- 使用 javascript 在页面加载时设置默认引导日期和日期时间值
- 为损坏的图像临时设置默认图像
- 我可以为高图表中的所有文本设置默认颜色吗?
- ExtJS xtype timefield - 设置默认选定
- Bootstrap 3 Datepicker v4-设置默认日期
- 如何设置默认视图位置(铯1.6)
- 聚合物:如何为具有嵌套特性的图纸选项卡设置默认值
- 使用复杂对象上的删除为选择框手动设置默认值
- Firefox插件:如何根据平台设置默认首选项
- DataTables设置默认排序列并设置无法排序的列