Wednesday, 31 July 2013

ECMAScript 5: Tamper proofing objects

var o = {
foo: "bar"
};
// adding a new property normally:
o.hey = "guys";
// preventing further extensions:
Object.preventExtensions(o);
// silent error, or TypeError under strict mode:
o.anotherProp = "This will fail!";
// existing properties can still be changed:
o.hey = "gals";
var o = {
foo: "bar"
};
// adding a new property normally:
o.hey = "guys";
// deleting a property normally:
delete o.hey;
// sealing the object:
Object.seal(o);
// silent failure, or TypeError under strict mode:
o.anotherProp = "This will fail!";
// existing properties can still be changed if they were writable:
o.foo = "baz";
// silent failure, or TypeError under strict mode:
delete o.foo;
//We can do everything Object.seal does, but also protect data properties
//from being overwritten by using the Object.freeze method.
var o = {
foo: "bar"
};
// adding a new property normally:
o.hey = "guys";
// deleting a property normally:
delete o.hey;
// sealing the object:
Object.freeze(o);
// silent failures, or TypeErrors under strict mode:
o.anotherProp = "This will fail!";
o.foo = "baz";
delete o.foo;
var o = { foo: "bar" };
Object.preventExtensions(o);
Object.defineProperty(o, "foo", {
writable: false,
configurable: false
});
// Object.isFrozen(o) === true
//http://www.jimmycuadra.com/posts/ecmascript-5-tamper-proofing-objects
view raw gistfile1.js hosted with ❤ by GitHub

No comments:

Post a Comment