EcmaScript Fifth Edition

Object methods

Object.getOwnPropertyDescriptor(obj, prop): a property has either a value and three boolean attributes writable, enumerable and configurable, or a get-ter and a set-ter.

  value: "foo",
  writable: true,     // You can modify the value.
  enumerable: true,   // Will appear in a "for (var i in obj.prop)".
  configurable: true  // You can change "writable", "enumerable" and
                      // "configurable"

Object.defineProperty(obj, prop, desc): set the property descriptor (as seen above).

Object.defineProperty(obj, 'answer', {
  get: function() { return 42; },
  set: function() { throw Error('Truth is immutably 42'); }

Object.defineProperties(obj, props): same as above.

Object.defineProperties(obj, {
  'prop1': {value: 1337, writable: false},
  'prop2': {value: 666, enumerable: false}

Object.keys(obj): array of strings of all enumerable properties.

Object.getOwnPropertyNames(obj): array of strings of all properties (including non-enumerable ones).

Object.create(proto, props): inherit a new object from the object proto.

var mum = Object.create({}, {common: {value:5}, overriden: {value:7}});
var baby = Object.create(mum, {overriden: {value:8}});

Object.getPrototypeOf(obj): self-descriptive.

Object.preventExtensions(obj): now you can't add properties to obj.


Object.seal(obj): now you can't delete or add any properties, and you can't change their descriptors (but you can change their values).


Object.freeze(obj): now you can't delete, add, or change any property.


Array awesomeness

Strict mode

How to use it? Write the following instruction:

"use strict";

What does it do?



Worth noting

Standard available at ECMA.

My dearest thanks go to Brendan Eich, Allen Wirfs-Brock, Douglas Crockford, Mark Miller, Tom Van Cutsem, Alex Russell, and so many others.