【发布时间】:2019-12-24 09:13:22
【问题描述】:
循环内的对象与外部的对象具有不同的值。我似乎无法找出值如何变化。
f = console.log
a = ["b","c"]
o ={d:1,e:2}
a.forEach(x=>{
o.k = x
window[x]=o;
f("inside the loop we have "+ x + ":")
f(window[x])
})
f("outside the loop we have b:")
f(b)
f("outside the loop we have c:")
f(c)
奇怪的是输出是:
inside the loop we have b:
Object { d: 1, e: 2, k: "b" }
inside the loop we have c:
Object { d: 1, e: 2, k: "c" }
outside the loop we have b:
Object { d: 1, e: 2, k: "c" }
outside the loop we have c:
Object { d: 1, e: 2, k: "c" }
为什么值b.k 更改为"c"?如果觉得这很奇怪。但显然我在这里没有什么。
【问题讨论】:
标签: javascript loops object