We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"==" 允许在相等比较中进行强制类型转换,而 "===" 不允许
注: ToPrimitive 抽象操作的所有特性(如 toString()、valueOf()) 在这里都适用。
ToPrimitive 运算符接受一个值,和一个可选的 期望类型 作参数。ToPrimitive 运算符把其值参数转换为非对象类型。如果对象有能力被转换为不止一种原语类型,可以使用可选的 期望类型 来暗示那个类型。
对Object进行ToPrimitive()操作: 返回该对象的默认值。对象的默认值由把期望类型传入作为hint参数调用对象的内部方法[[DefaultValue]]得到。
"0" == "" // false "0" == [] // false false == "" // true false == "" // true false == [] // true false == {} // false "" == 0 //true "" == [] // true "" == {} // false 0 == [] // true 0 == {} // false
更改内置原生原型
Number.prototype.valueOf = function() { return 3; }; new Number( 2 ) == 3; // true
http://yanhaijing.com/es5/#203 http://yanhaijing.com/es5/#103 http://yanhaijing.com/es5/#100 https://zhuanlan.zhihu.com/p/29730094
The text was updated successfully, but these errors were encountered:
No branches or pull requests
宽松相等(抽象相等)== 和严格相等 === 正确的解释:
"==" 允许在相等比较中进行强制类型转换,而 "===" 不允许
抽象相等
规则:
有几个非常规的情况需要注意。
即视为相等,不发生强制类型转换。
比较:
字符串被强制类型转换为数字以便进行相等比较。
布尔类型被强制类型转换为数字,然后进行下一步判断。
(1) 如果 x 为 null,y 为 undefined,则结果为 true。
(2) 如果 x 为 undefined,y 为 null,则结果为 true。
(1) 如果 Type(x) 是字符串或数字,Type(y) 是对象,则返回 x == ToPrimitive(y) 的结果;
(2) 如果 Type(x) 是对象,Type(y) 是字符串或数字,则返回 ToPromitive(x) == y 的结果。
注: ToPrimitive 抽象操作的所有特性(如 toString()、valueOf()) 在这里都适用。
ToPrimitive
ToPrimitive 运算符接受一个值,和一个可选的 期望类型 作参数。ToPrimitive 运算符把其值参数转换为非对象类型。如果对象有能力被转换为不止一种原语类型,可以使用可选的 期望类型 来暗示那个类型。
对Object进行ToPrimitive()操作: 返回该对象的默认值。对象的默认值由把期望类型传入作为hint参数调用对象的内部方法[[DefaultValue]]得到。
[[DefaultValue]] (hint)
例子
比较特殊的情况
更改内置原生原型
参考
http://yanhaijing.com/es5/#203
http://yanhaijing.com/es5/#103
http://yanhaijing.com/es5/#100
https://zhuanlan.zhihu.com/p/29730094
The text was updated successfully, but these errors were encountered: