Skip to content
New issue

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

第10题(2019-08-02):输出以下代码运行结果 #4

Open
qappleh opened this issue Aug 2, 2019 · 3 comments
Open

第10题(2019-08-02):输出以下代码运行结果 #4

qappleh opened this issue Aug 2, 2019 · 3 comments
Labels

Comments

@qappleh
Copy link
Owner

qappleh commented Aug 2, 2019

1 + "1"

2 * "2"

[1, 2] + [2, 1]

"a" + + "b"  
@qappleh
Copy link
Owner Author

qappleh commented Aug 5, 2019

答案:
11
4
1,22,1
aNaN

解析:
1 + "1"
加性操作符:如果只有一个操作数是字符串,则将另一个操作数转换为字符串,然后再将两个字符串拼接起来

所以值为:“11”

2 * "2"
乘性操作符:如果有一个操作数不是数值,则在后台调用 Number()将其转换为数值。

所以值为4

[1, 2] + [2, 1]
Javascript中所有对象基本都是先调用valueOf方法,如果不是数值,再调用toString方法。

所以两个数组对象的toString方法相加,值为:"1,22,1"

"a" + + "b"
后边的“+”将作为一元操作符,如果操作数是字符串,将调用Number方法将该操作数转为数值,如果操作数无法转为数值,则为NaN。

所以值为:"aNaN"

以上均参考:《Javascript高级程序设计》

@qappleh qappleh added the js label Nov 27, 2019
@ustchcl
Copy link

ustchcl commented Jun 22, 2020

4f14f5d

@qiqingfu
Copy link

qiqingfu commented Aug 1, 2020

@qappleh 顶一个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants