Changes to properties

suggest change

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents