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

以下代码最终在控制台输出的结果为__________。 #267

Open
pwstrick opened this issue Jul 16, 2019 · 3 comments
Open

以下代码最终在控制台输出的结果为__________。 #267

pwstrick opened this issue Jul 16, 2019 · 3 comments
Labels
JavaScript JavaScript类的题目

Comments

@pwstrick
Copy link
Owner

以下代码最终在控制台输出的结果为__________。

var a = {},
  b = { name: "ping" },
  c = { name: "wen" };
a[b] = 10;
a[c] = 20;
console.log(a[b]);
@pwstrick pwstrick added the JavaScript JavaScript类的题目 label Jul 16, 2019
@MssText
Copy link

MssText commented Jul 10, 2022

为啥结果是20呢?我理解b和c都是不同的引用地址,那么作为a的key应该也是不同的,感觉a[b]等于10才说得过去呀

@pwstrick
Copy link
Owner Author

@MssText a[b]和a[c] 中的 b 和 c 会被类型转换成字符串,相当于调用 ({ name: "ping" }).toString(),得到 '[object Object]',两个key是相同的,所以后者会覆盖前者

@MssText
Copy link

MssText commented Jul 10, 2022

@pwstrick 嗯嗯 js真是太多隐式行为了。。。。。

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

No branches or pull requests

2 participants