小编Lea*_*des的帖子

Javascript - 如何更改对象属性而不重新分配它们

下面的片段揭示了疑问

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

6
推荐指数
2
解决办法
1935
查看次数

标签 统计

javascript ×1