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
加法只有两种情况,数字加数字,字符串加字符串,否则一定会转成这两种类型
1. 将两个操作数转为原始类型 ToPrimitive(Date转为String,其他转为Number) 1.1 如果是原始值直接返回 1.2 如果是对象则调用valueOf方法,valueOf返回的是原始类型,则直接返回 1.3 否则调用对象的toString方法,如果返回原始类型,则直接返回 1.4 否则,报错 2. 如果其中一个是字符串,则另一个也转为字符串 3. 否则就转为两个数字,求和
// 例子1 []+[] 第一步:转为原始类型,[].valueOf() 还是 [],再调用[].toString(),返回"" 第二步:两个字符串 ""+""=""
// 例子2 []+{} 同理:[]是“”,{}.valueOf()报错,({}).valueOf()是{},({}).toString()是"[object Object]" 所以""+"[object Object]"="[object Object]"
// 例子3 {}+[] 前一个参数会被当成是代码块,也就是实际执行的是+[],这里的+是一元运算符,会把参数转为Number Number([])=>Number([].valueOf())=>Number([].toString())=>Number("")=>0
// 例子4 ({})+[] 同例子2
// 例子5 "9"+1 = 第一步 转为原始类型,结果是都不用转 第二步 如果有一个是字符串,另一个也转为字符串,则"9"+"1"=="91"
// 例子6 “9”-1 减法都转为Number,9-1==8
参考文章1:JavaScript中,{}+{}等于多少? 参考文章2:jsfuck
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
No branches or pull requests
加法只有两种情况,数字加数字,字符串加字符串,否则一定会转成这两种类型
参考文章1:JavaScript中,{}+{}等于多少?
参考文章2:jsfuck
The text was updated successfully, but these errors were encountered: