JavaScript类型转换:(true \u0026\u0026 1)vs(true | | 1)

栏目: JavaScript · 发布时间: 7年前

内容简介: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转换规则,我希望在这两种情况下都会得到布尔值,因为表达式的布尔上下文.

检查 Douglas Crockford’s site ,它说:
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)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Flash第一步

Flash第一步

陈冰 / 清华大学出版社 / 2006-3 / 45.00元

《Flash第1步:ActionScript编程篇》(珍藏版)为《Flash第一步》的ActionScript编程篇,包含后4部分内容。第3部分为ActionScript篇,你将学会像一个软件设计师那样来思考问题,并掌握在Flash中进行程序开发工作所必须具备的重要知识,还将学会运用Flash完整的编程体系来完成从简单到复杂的各种编程任务。另外,在开发一个Flash应用过程中会涉及的各种其他Web......一起来看看 《Flash第一步》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换