OBJECTS
In strict mode, object manipulation is more likely to throw errors than in nonstrict mode. Strict mode tends to throw errors in situations where nonstrict mode silently fails, increasing the likelihood of catching an error early on in development.
To begin, there are several cases where attempting to manipulate an object property will throw an error:
- Assigning a value to a read-only property throws a
TypeError
. - Using
delete
on a nonconfigurable property throws aTypeError
. - Attempting to add a property to a nonextensible object throws a
TypeError
.
Another restriction on objects has to do with declaring them via object literals. When using an object literal, property names must be unique. For instance:
// Two properties with the same name
// Non-strict mode: No error, second property wins
// Strict mode: Throws a syntax error
let person = {
name: "Nicholas",
name: "Greg"
};
The object literal for person
has two properties called name
in this code. The second...