【发布时间】:2014-12-19 11:54:37
【问题描述】:
我有以下 Ecma-Script-6 模板常量的代码。
const person = 'John Smith';
console.log(person);
person = 'Naeem Shaikh';
console.log("{{After changing the value for person.name}}");
console.log(person);
当然不行。 http://www.es6fiddle.net/i3vhumdx/ 出现以下错误,
person is read-only
现在我对一个对象做同样的事情。
const person = {name: 'John Smith'};
console.log(JSON.stringify(person));
person.name='Naeem Shaikh';
person.age="24";
console.log("{{After changing the value for person.name}}");
console.log(JSON.stringify(person));
http://www.es6fiddle.net/i3vhzblm/
输出是:
{"name":"John Smith"}
{{After changing the value for person.name}}
{"name":"Naeem Shaikh","age":"30"}
在这里我可以毫无问题地写入只读对象。谁能解释这种行为。
【问题讨论】:
标签: javascript node.js ecmascript-6