好奇以下两种情况:
第一的:
const key = 2;
const obj = { "2": "stackoverflow" };
obj[key]; //results in "stackoverflow" but the key is 2 and not "2"
第二:
//Vice versa case
const otherKey = "21";
const otherObj = { 21: "youtube" };
otherObj[otherKey]; //results in "youtube" but the key is "21" and not 21
我的结论:
由于键应该是字符串,并且key在 Javascript 对象中查找(当键似乎是数字时)存在时,它通过类型转换比较而不是通过严格或字符串转换来实现。
这些案例是否有效,而且我对其中的部分更感兴趣?
阿波罗的战车
MMTTMM
肥皂起泡泡
相关分类