尝试修改冻结对象时可能会抛出错误
Possible to throw an error when trying to modify frozen object
如果我 运行 这样的代码:
var a = { prop: 123 };
Object.freeze(a);
a.prop = 456;
正如预期的那样,a.prop
仍然是“123”,因为对象被冻结了。我想知道的是,在尝试修改冻结对象的 属性 时是否有可能让 运行 时间抛出错误?
与往常一样,秘诀是 "use strict"
mode 不忽略异常:
"use strict";
var a = Object.freeze({ prop: 123 });
a.prop = 456;
如果我 运行 这样的代码:
var a = { prop: 123 };
Object.freeze(a);
a.prop = 456;
正如预期的那样,a.prop
仍然是“123”,因为对象被冻结了。我想知道的是,在尝试修改冻结对象的 属性 时是否有可能让 运行 时间抛出错误?
与往常一样,秘诀是 "use strict"
mode 不忽略异常:
"use strict";
var a = Object.freeze({ prop: 123 });
a.prop = 456;