简体   繁体   English

object 冻结的奇怪行为

[英]strange behavior of object freeze

I'm trying to use Object freeze function in node v13.10.1, however, it behaves remarkably useless?我正在尝试在节点 v13.10.1 中使用 Object freeze function,但是,它的行为非常无用? what's the point if I'm not get anything if somebody try to change a frozen attribute, they might think that they've changed it and search for the cause of confusion, exactly like me!如果有人尝试更改冻结的属性,如果我没有得到任何东西,那有什么意义,他们可能会认为他们已经更改了它并寻找混乱的原因,就像我一样!

for instance this code does not throw error!!例如此代码不会引发错误! uncanny!不可思议!

const myObj = Object.freeze({
  name: 'alex',
});

myObj.name = 'tom';

console.log(myObj); // it'll show { name: 'alex' }

because by default you're running your code in sloppy mode因为默认情况下你在草率模式下运行你的代码

you should declare strict mode , so run this one instead to see你应该声明严格模式,所以运行这个来代替

 'use strict'; const myObj = Object.freeze({ name: 'alex' }); myObj.name = 'tom';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM