Changes to properties
suggest changeStrict mode also prevents you from deleting undeletable properties.
"use strict";
delete Object.prototype; // throws a TypeError
The above statement would simply be ignored if you don’t use strict mode, however now you know why it does not execute as expected.
It also prevents you from adding a property to a non-extensible object. In non-strict code the assignment is ignored. In strict mode it throws. These have to be separate functions: a second function setAge would replace the first.
const myObject = {name: "My Name"};
Object.preventExtensions(myObject);
function setAgeSloppy() {
myObject.age = 25; // Ignored in non-strict code. A module is strict, so this would throw.
}
function setAgeStrict() {
"use strict";
myObject.age = 25; // TypeError: object is not extensible
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents