Comments (5)
简单说明
== 相等运算符(The Equals Operator),
=== 严格相等运算符(The Strict Equals Operator)
为了方便说明, 在此定义以下简称以便于区分:
{
"===": "全等",
"==": "双等"
}
两种操作最为明显的区别在于 全等要求两个操作数类型相同(typeof),
而且在==规则中, 如果俩操作数的typeof结果相同, 则使用===规则比较(规则第1条)
from blog.
全等规则: x === y 的比较逻辑
-
判断类型是否相同
- 不同 ->
false
- 相同 继续进行以下步骤
- 不同 ->
-
x和y
- 同为undefined ->
true
- -> 继续
- 同为undefined ->
-
x和y
- 同为null ->
true
- -> 继续
3.1. x,y同为number类型, 则:
- x或y任意一方为NaN ->
false
- x和y的数值相等 ->
true
- 0与-0 ->
true
- 两数值不想等 ->
false
3.2. 同为string, 则相同位置上的字符对应的16bit作比较
- 所有位置全相同 ->
true
- ->
false
3.3. 同为boolean,
- true:true ->
true
- false:false ->
true
- (true, false) ->
false
3.4. 同为引用类型,
- 指向同一个对象 ->
true
- ->
false
- 同为null ->
from blog.
双等规则: x == y 的比较逻辑
x, y 位置可互换, 即在此表示 x == y 与 y == x
- typeof类型相等, 则直接用全等规则,
- 如果(x, y) 是(undefined, null)的组合关系 - ->
true
即 x:y undefined:null 或 null:undefined, - (number, string), string转换为number, 再使用 全等规则 比较,
- x 是boolean, 先把它转换成number, 再递归本 双等规则 即
toNumber(x) == y
x: true -> 1, false -> 0 - x 是 boolean/number/string, y 是 object, 递归比较
x == y.toPrimitive()
, toPrimitive = y.valueOf() || y.toString(), - ->
false
from blog.
特别说明
关于双等规则 第5条, object的.valueOf优先级比.toString要高, 所以 toPrimitive = y.valueOf || y.toString的描述是有效的.
from blog.
plus: +
two number
- Infinity + Infinity = Infinity
- -Infinity + -Infinity = Infinity
- Infinity +/- Infinity = NaN
- NaN + X = NaN (undefined + undefined = NaN)
- 1 + 2 = 3
str + x, x + str
- x is a number(N, NaN, Infinity, -Infinity):
${str}${x}
,${x}${str}
- x is a string:
${str}${x}
,${x}${str}
- x is a boolean: x => 'true' or 'false':
${str}${x}
,${x}${str}
- x is null or undefined: null => 'null', undefined => 'undefined', then
${str}${x}
,${x}${str}
- x is a object:
${str}${object.toString()}
,${object.toString()}${str}
, 对象默认toString()
来自于 Object.prototype.toString()
Object
- undefined + undefined = NaN
- NaN + undefined = NaN
- NaN + NaN = NaN
- a.toString() + b.toString()
from blog.
Related Issues (20)
- HTML: JS: meta-viewport HOT 1
- JS: snake case & camel case HOT 1
- Docker: WordPress, Nginx HOT 2
- Nuxt: [BABEL] Note: The code generator has deoptimised the styling of XXX.js as it exceeds the max of 500KB. HOT 1
- Nginx: Tips HOT 2
- Shell: Tips HOT 1
- Nacos: Tips HOT 2
- Xnix: LVM: Tips
- OS: Ubuntu: apt HOT 1
- OS: Ubuntu: xclip HOT 3
- OS: Ubuntu: user HOT 1
- OS:Ubuntu: Nvidia GPU HOT 5
- OS: Ubuntu: CUDA
- OS:Ubuntu:zshconfig
- Web: blogs
- Network: tools: curl
- PL: node.js: egg.js HOT 2
- FFMPEG: ProRes HOT 1
- Networking: 阿里云: 云解析DNS HOT 3
- Shell: scripts HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.