我正在审查angularjs工厂的代码,以便更好地了解它的工作原理.该代码包含一个if我不完全理解的声明.
在一个plnkr演示中,作者写道:
if ((+!!config.template) + (+!!config.templateUrl) !== 1) {
throw new Error('Expected modal to have exactly one of either `template` or `templateUrl`');
}
Run Code Online (Sandbox Code Playgroud)
if (!(!config.template ^ !config.templateUrl)) {
throw new Error('Expected modal to have exactly one of either `template` or `templateUrl`');
}
Run Code Online (Sandbox Code Playgroud)
显然,通过错误消息,它正在检查两者中是否存在其中一个.我只是不确定它是如何得出结论的.我无法找到任何关于^或的信息+!
我的问题是:这个if语句如何工作?(^或+!或+!!特异性)
Mat*_*all 72
!!将值转换为布尔值(true或false). +然后转换该布尔值的数,或者1为true或0为假.
> +true
1
> +false
0
Run Code Online (Sandbox Code Playgroud)
就个人而言,在处理两个布尔值时,我发现写这样的东西更清楚:
if (!config.template == !config.templateUrl) {
throw ...
}
Run Code Online (Sandbox Code Playgroud)
显然,代码清晰度和可读性受到了谴责.
Tra*_*s J 33
+!使用隐式转换将值转换为0或1,具体取决于其布尔值
在大多数情况下,这是为了检查是否存在.例如,空字符串是false(!!"" === false),因此未定义,还有许多其他字符串.这些是主要的两个
"虚假"转换
+!!"" === 0
+!!false === 0
+!!0 === 0
+!!undefined === 0
+!!null === 0
+!!NaN === 0
Run Code Online (Sandbox Code Playgroud)
"Truthy"转换
+!!1 === 1
+!!true === 1
+!!"Foo" === 1
+!!3.14 === 1
+!![] === 1
+!!{} === 1
Run Code Online (Sandbox Code Playgroud)
if((+ !! config.template)+(+ !! config.templateUrl)!== 1)
希望这在这一点上更有意义.该对象config有两个我们正在研究的属性..template和.templateUrl.隐式转换为0或1使用+!!将被添加,然后进行比较以确保它不是1(这意味着它是0或2) - 属性既可以打开也可以关闭但不同.
这里的真值表如下:
template templateUrl (+!!) + (+!!) !==1
"foo" "foo" 1 + 1 true
undefined undefined 0 + 0 true
undefined "" 0 + 0 true
"" undefined 0 + 0 true
12 "" 1 + 0 false
"" 12 0 + 1 false
undefined "foo" 0 + 1 false
"" "foo" 0 + 1 false
"foo" "" 1 + 0 false
"foo" undefined 1 + 0 false
Run Code Online (Sandbox Code Playgroud)
所有这一切的一个更简单的方法就是使用隐式布尔转换
if (!config.template === !config.templateUrl)
Run Code Online (Sandbox Code Playgroud)
Ste*_*her 31
这是一种可怕的可读方式,用于写出变量的布尔值,然后使用一元转换将其转换为0/1数字结果.
考虑:
+!!true; //this converts true to false, then to true again, and true is 1 when converted
+!!0; //converts 0 (falsy) to true, then false, and then the numeric 0
Run Code Online (Sandbox Code Playgroud)
从技术上讲!!,它不是它自己的运算符,它只是NOT(!)运算符两次.
一元转换:ECMA规范doc一元加上尝试转换为整数.Number()也将是一个有效的转换.
| 归档时间: |
|
| 查看次数: |
3102 次 |
| 最近记录: |