我正在查看angularjs工厂的代码,以更好地了解其工作方式。该代码包含一个if
我不完全理解的语句。
在plnkr演示中,作者写道:
if ((+!!config.template) + (+!!config.templateUrl) !== 1) {
throw new Error('Expected modal to have exactly one of either `template` or `templateUrl`');
}
if (!(!config.template ^ !config.templateUrl)) {
throw new Error('Expected modal to have exactly one of either `template` or `templateUrl`');
}
显然,通过错误消息,它正在检查是否存在两者之一。我只是不确定如何得出结论。我无法找到有关^
或的任何信息+!
我的问题是:如果声明有效,这如何处理?(^
或+!
或+!!
特异性)
!!
将值转换为布尔值(true
或false
)。 +
然后转换该布尔值的数,或者1
为true
或0
为假。
> +true
1
> +false
0
就个人而言,当处理两个布尔值时,写这样的代码会更清晰:
if (!config.template == !config.templateUrl) {
throw ...
}
显然,代码的清晰度和可读性受到了谴责。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句