Skip to content

Instantly share code, notes, and snippets.

@luongs3
Created January 1, 2020 03:36
Show Gist options
  • Save luongs3/628b3a1ff1049158e85a9760481501ba to your computer and use it in GitHub Desktop.
Save luongs3/628b3a1ff1049158e85a9760481501ba to your computer and use it in GitHub Desktop.
// Without Strict Mode
NaN = "lol"; // Nothing happen
var obj = {};
Object.defineProperty(obj, 'prop', {value: 1, writable:false});
obj.prop; // => 2
obj.prop = 10;
obj.prop; // => 2
// With Strict Mode
"use strict";
NaN = "wtf"; // TypeError
var obj = {};
Object.defineProperty(obj, 'prop', {value: 1, writable:false});
obj.prop; // => 2
obj.prop = 10; // Uncaught TypeError: Cannot assign to read only property 'prop' of object #<Object>
// Assignment to a getter-only property
var obj = { get x() { return 1; } };
obj.x = 2; // Uncaught TypeError: Cannot set property x of #<Object> which has only a getter
// Assignment to a new property on a non-extensible object
var fixedObj = {};
Object.preventExtensions(fixedObj);
fixedObj.newProp = "new value"; // Uncaught TypeError: Can't add property newProp, object is not extensible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment