Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Created November 19, 2014 11:54
Show Gist options
  • Save shaunwallace/f1e24d2096166dfad4b4 to your computer and use it in GitHub Desktop.
Save shaunwallace/f1e24d2096166dfad4b4 to your computer and use it in GitHub Desktop.
Converting mistakes in errors - Strict Mode
// Assignment to a non-writable property
var obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // throws a TypeError
// Assignment to a getter-only property
var obj2 = { get x() { return 17; } };
obj2.x = 5; // throws a TypeError
// Assignment to a new property on a non-extensible object
var fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // throws a TypeError
// It also makes it impossible to delete object properties that are undeletable
delete Object.prototype; // throws a TypeError
// Forbids deleting plain names such as
var x;
delete x; // syntax error
// Requires all properties names in an object literal to be unique instaed of the last one winning
var obj3 = { foo : true, foo : false }; // syntax error
// Requires function parameter names to be unique instaed of the last duplicated parameter hiding the previous paramter
function sum( a, b, a ) {
return a + b + c; // snytax error
}
// prevents octoal syntax
var mult = 023 * 50; // syntax error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment