下面的片段揭示了疑问
var foo = 'something'
var baz = 'other thing'
var obj = {
prop1 : 'my prop',
prop2 : foo, //referencing external variable
prop3 : baz //referencing external variable
}
// here we get the expected obj to be printed
console.log(obj)
// now I change one of the initial variable
foo = 'changed'
// here we get the sabe print as the first, that is the ~problem~
console.log(obj)
Run Code Online (Sandbox Code Playgroud)
那么,如何在没有重新分配的情况下在prop2上打印"已更改" obj.prop2 = foo
javascript ×1