JavaScript类型转换:(true \u0026\u0026 1)vs(true | | 1)
栏目: JavaScript · 发布时间: 6年前
内容简介:http://stackoverflow.com/questions/8559920/javascript-type-conversion-true-1-vs-true-1
JavaScript是非严格类型的 Java 语言,例如.
我们知道,它会根据上下文来转换结果的值:
“2”“3”结果“23”
“2”*“3”结果6
这是非常明确的,可以理解.
我只是尝试了以下表达式,并被困惑:
真实&& 1结果1
真|| 1个结果是真的
为什么第一个给数字和第二个给出布尔值?
考虑到JavaScript转换规则,我希望在这两种情况下都会得到布尔值,因为表达式的布尔上下文.
The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, “” (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:
var value = p && p.name; /* The name value will only be retrieved from p if p has a value, avoiding an error. */
The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, “” (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:
value = v || 10; /* Use the value of v, but if v doesn't have a value, use 10 instead. */
http://stackoverflow.com/questions/8559920/javascript-type-conversion-true-1-vs-true-1
以上所述就是小编给大家介绍的《JavaScript类型转换:(true \u0026\u0026 1)vs(true | | 1)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法设计与分析导论
R.C.T.Lee (李家同)、S.S.Tseng、R.C.Chang、Y.T.Tsai / 王卫东 / 机械工业 / 2008-1 / 49.00元
本书在介绍算法时,重点介绍用干设计算法的策略.非常与众不同。书中介绍了剪枝搜索、分摊分析、随机算法、在线算法以及多项式近似方案等相对较新的思想和众多基于分摊分析新开发的算法,每个算法都与实例一起加以介绍,而且每个例子都利用图进行详细解释。此外,本书还提供了超过400幅图来帮助初学者理解。本书适合作为高等院校算法设计与分析课程的高年级本科生和低年级研究生的教材,也可供相美科技人员和专业人七参考使用。一起来看看 《算法设计与分析导论》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
HSV CMYK 转换工具
HSV CMYK互换工具