forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrict_mode.js
More file actions
24 lines (19 loc) · 770 Bytes
/
Copy pathstrict_mode.js
File metadata and controls
24 lines (19 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"use strict";
// examples taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
// Assignment to a non-writable global
const undefined = 5; // throws a TypeError
const Infinity = 5; // throws a TypeError
// Assignment to a non-writable property
const obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // throws a TypeError
// Assignment to a getter-only property
const obj2 = { get x () { return 17; } };
obj2.x = 5; // throws a TypeError
// Assignment to a new property on a non-extensible object
const fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // throws a TypeError
// deleting undeletable properties
"use strict";
delete Object.prototype; // throws a TypeError