JS隐式转换一道面试题

温故而知新

const a = {};
const b = {key: 'b'};
const c = {key: 'c'}:

a[b] = 123;
a[c] = 456;

console.log(a[b]);

结果打印为 456
原因,对象属性只能是字符串类型,a[b]时会把b对象toString === "[object Object]"
所以在a[b] === a[c] === a["[object Object]"], 所以最后输出为 456

本文链接:

http://godrry.com/archives/js-an-interview-question.html
1 + 9 =
沙发还热乎呢~