日期:2014-05-16  浏览次数:20335 次

javascript语法的问题:严格等于和等于区别
The Undefined type 未定义类型the Undefined type has only one value, undefined. When a variable is declared and not initialized, it is given the value of undefined by default.
未定义类型,只有一个值undefined只要你定义了变量没有赋值,那么它的值就是undefined

The Null type 空类型Another type with just one value, the Null type, has only the special value null, which is also its literal. The value undefined is actually a derivative of the value null, so ECMAScript defines them as equal to each other.
空类型也只有一个值null,未定义类型的值undefined 其实是空类型值null的一个衍生值,所以ECMAScript定义他俩相等

Equal and not equal 等于和不等于The equal operator in ECMAScript is the double equal sign (==), and it returns true if—and only if—both operands are equal. The not equal operator is the exclamation point followed by an equal sign (!=), and it returns true if—and only if—two operands are not equal. Both operators do conversions in order to determine if two operands are equal. 等于和不等于用法和其他类型语言大致相同,但是又有很多不同,比如,你在比较相同的字符数字和数字类型数字式会“自动转换”后比较。

毕竟JS是弱类型语言,一些特例在下面
1.null == undefined true
2.“NaN” == NaN false
3.5 == NaN false
4.NaN == NaN false
5.NaN != NaN true
6.false == 0 true
7.true == 1 true
8.true == 2 false
9.undefined == 0 false
10.null == 0 false
11.“5” == 5 true
Identically equal and not identically equal全等(值和类型)和不全等The brothers of the equal and not equal operators are the identically equal and not identically equal operators. These two operators do the same thing as equal and not equal, except that they do not convert operands before testing for equality. The identically equal operator is represented by three equal signs (===) and only returns true if the operands are equal without conversion. 一句话解释:用===和!==,比较前做比较的值不会做转换。

至于内存地址比较说,我不敢苟同,JavaScript是弱类型,虽然分着几个类型的,如String,Number,Boolean,Object,我个人认为,==和===都应该是值比较,至于用==产生的特例情况应当是ECMAScript设计之初就加进去的特性,===是instanceof && ==
也就是说===更符合强类型语言规范

附送一个(==)转换规则 When performing conversions, follow these basic rules:

? If an operand is a Boolean value, convert it into a numeric value before checking for equality.A value of false converts to 0; whereas a value of true converts to 1.

? If one operand is a string and the other is a number, attempt to convert the string into a number before checking for equality.

? If one operand is an object and the other is a string, attempt to convert the object to a string (using the toString() method) before checking for equality.

? If one operand is an object and the other is a number, attempt to convert the object to a number before checking for equality.