The new ECMAScript 5 standard brings on the table some very nice features you can use right away in the modern browsers. The presentation is a big overview of them.
The new ECMAScript 5 standard brings on the table some very nice features you can use right away in the modern browsers. The presentation is a big overview of them.
var obj = Object.create({});obj.__proto__.__proto__ === Object.prototype; // true (ES3way)
btw. there isObject.isPrototypeOf
var obj = Object.create({});Object.prototype.isPrototypeOf(obj); // true same asvar obj = Object.create({});obj.__proto__.__proto__ === Object.prototype; // true (ES3 way)
everything starts withObject.prototype, I’m sure you lied!
var obj = Object.create(null);Object.prototype.isPrototypeOf(obj); // false
isPrototypeOf is even from ECMA 3!
Object.getPrototypeOf is a new stuff however...
var obj = Object.create({});Object.getPrototypeOf(obj) === obj.__proto__; // true
with Object.getPrototypeOf you simply forget aboutnon-standard obj.__proto__
var fn = function() {};fn.prototype.foobar = 1;var obj = new fn();Object.getPrototypeOf(obj) === fn.prototype;
var obj = Object.create({});Object.prototype.isPrototypeOf(obj); // true same asvar obj = Object.create({});Object.getPrototypeOf(Object.getPrototypeOf(obj)) ===Object.prototype; // true// no __proto__!
Ok, you won. Tell me more about Object.create!
if (!Object.create) { Object.create = function (o) { if (arguments.length > 1) { throw new Error(Object.create implementation onlyaccepts the first parameter.); } function F() {}; F.prototype = o; return new F(); };}
wow, we found a polyfill!
inheritance?
var animal = Object.create({ age : null, setAge: function(age) { this.age = age; }});var bird = Object.create(animal);bird.setAge(10);bird.age; // 10
give me analogy, I still remember ECMA3
var Animal = function() {};Animal.prototype = { age : null, setAge: function(age) { this.age = age; }};var Bird = function() {};Bird.prototype = new Animal();var bird = new Bird();bird.setAge(10);bird.age; // 10
more analogies?
var Animal = function() {};var obj = new Animal();var obj2 = Object.create(Animal.prototype);obj2.constructor === obj.constructor; // true
not enough, you know that
var obj = {};// does the same job asvar obj2 = Object.create(Object.prototype);
var obj = {};Object.defineProperty(obj, "foobar", { value : 100, writable : false});obj.foobar; // 100obj.foobar = 200;obj.foobar; // 100
writable: falseforget about changing property’s value
enumerable?!
var obj = {};Object.defineProperty(obj, "foobar", { value : 100, enumerable : false});for (var i in obj) { console.log(obj[i]);}// nothing happened
however...
var obj = {};Object.defineProperty(obj, "foobar", { value : 100, enumerable : true !});for (var i in obj) { console.log(obj[i]);}// „foobar” !
configurable?!
var obj = {};Object.defineProperty(obj, "foobar", { value : 100, configurable : false});delete obj.foobar; // falseobj.foobar; // 100
var obj = {};Object.defineProperty(obj, "foobar", { value : 100, configurable : false}); // it’s ok, we just defined a foobar property not to be configurableObject.defineProperty(obj, "foobar", { configurable : true}); // throws a TypeErrorObject.defineProperty(obj, "foobar", { writable : true}); // throws a TypeErrorObject.defineProperty(obj, "foobar", { enumerable : true}); // throws a TypeError
Summary of descriptors
var obj = {};obj.foobar = 100; same asObject.defineProperty(obj, "foobar", { value : 100, writable : true, configurable : true, enumerable : true});
Object.defineProperty(obj, "foobar", { value : 100}); same asObject.defineProperty(obj, "foobar", { value : 100, writable : false, configurable : false, enumerable : false});
IE8 allows you use it only with DOM objects Summary of descriptors
Oh, there are setters and getters
setters
var rect = { x: 5, y: 2};Object.defineProperty(rect, "area", { set: function(value) { throw new Error("Cannot set a value"); }, get: function() { return this.x * this.y; }});rect.area = -21; // Error: Cannot set a value
watch out
var rect = { x: 5, y: 2};Object.defineProperty(rect, "area", { set: function(value) { throw new Error("Cannot set a value"); }, get: function() { return this.x * this.y; }, writable: false // TypeError: Invalid property. A property cannot bothhave accessors and be writable or have a value: #<Object>});
getters
var rect = { x: 5, y: 2};Object.defineProperty(rect, "area", { set: function(value) { throw new Error("Cannot set a value"); }, get: function() { return this.x * this.y; }});rect.area; // 10 (5 * 2 = 10)
var arr = [1, 2, 3];Object.keys(arr); // ["0", "1", "2"]
Polyfill?
if(!Object.keys) { Object.keys = function(o) { if (o !== Object(o)) { throw new TypeError(Object.keys called on non-object); } var ret = []; var p; for (p in o) { if (Object.prototype.hasOwnProperty.call(o, p)) { ret.push(p); } } return ret; };}
Object.seal
var obj = { foo: 1};Object.seal(obj);obj.myEvilProperty = 1337;obj.myEvilProperty; // undefined
It prevents new properties from being added to the sealed object!
var obj = { foo: 1};Object.seal(obj);Object.defineProperty(obj, "foo", { value: "boom" }); // throws a TypeError
Object.seal makes all theproperties not configurable
var obj = { foo: 1};Object.seal(obj);Object.defineProperty(obj, "foo", { get: function() { return "g"; }}); // throws a TypeError
var obj = { foo: 1};Object.seal(obj);delete obj.foo; // does nothingobj.foo; // 1
Object.isSealed
var obj = { foo: 1};Object.seal(obj);Object.isSealed(obj); // true
Object.freeze
well, you can do nothing
Freezes an object: that is, prevents new properties frombeing added to it; prevents existing properties from beingremoved; and prevents existing properties, or theirenumerability, configurability, or writability, from beingchanged. In essence the object is made effectivelyimmutable. The method returns the object being frozen. - MDN - the best source of knowledge https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/freeze
var obj = { foo: 1};Object.freeze(obj);obj.myEvilProperty = 1337;obj.myEvilProperty; // undefined
var obj = { foo: 1};Object.freeze(obj);obj.foo = 1337;obj.foo; // 1
var obj = { foo: 1};Object.freeze(obj);Object.defineProperty(obj, "foo", { value: "boom" }); // throws a TypeError
var obj = { foo: 1};Object.freeze(obj);delete obj.foo; // false
Object.isFrozen
var obj = { foo: 1};Object.freeze(obj);Object.isFrozen(obj); // true
Object.preventExtensions
Simply, you can’t add new properties
var obj = { foo: 1};Object.preventExtensions(obj);obj.myEvilProperty = 1337;obj.myEvilProperty; // undefined
How it’s different than Object.seal?
You can remove properties!
var obj = { foo: 1};Object.seal(obj);delete obj.foo; // falsevar obj2 = { bar: 2};Object.preventExtensions(obj2);delete obj2.bar; // true
You can change descriptors!
var obj = { foo: 1};Object.seal(obj);Object.defineProperty(obj, "foo", { enumerable: false}); // throws a TypeErrorvar obj2 = { bar: 2};Object.preventExtensions(obj2);Object.defineProperty(obj2, "bar", { enumerable: false}); // its ok
Object.isExtensible
var obj = { foo: 1};Object.preventExtensions(obj);Object.isExtensible(obj); // false
var foo = {};foo.bar = "new property";foo.baz = 3;var JSONfoo = JSON.stringify(foo); // {"bar":"new property","baz":3}var foo = JSON.parse(JSONfoo); // yet again we have an object
strict mode
all of these throw errors"use strict";eval = 17;arguments++;++eval;var obj = { set p(arguments) { } };var eval;try { } catch (arguments) { }function x(eval) { }function arguments() { }var y = function eval() { };var f = new Function("arguments", "use strict; return 17;");"use strict";var fn = function() { return arguments.callee; };fn(); more: https://developer.mozilla.org/en/JavaScript/Strict_mode
1–1 of 1 previous next