简体   繁体   中英

strange behavior of object freeze

I'm trying to use Object freeze function in node v13.10.1, however, it behaves remarkably useless? 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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