sara usingers
sara usingers

Reputation: 43

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' }

Upvotes: 0

Views: 45

Answers (1)

Robot
Robot

Reputation: 991

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';

Upvotes: 3

Related Questions