What Is New in EcmaScript 5?

More and more browsers have implemented EcmaScript 5. That means that it is time to get up to speed on what it means for us as developers

Here is a summary of Douglas Crockfords talk on Ecmascript 5 at Scandev

New Syntax

  • It is now possible to use reserved words as property names and in
    unquoted literal keys.
  • A comma is allowed after the last pair in object literals and after
    the last element in array literals.
  • It is possible to break strings by inserting a backslash at the end of
    the line (not recommended by Doug).

object.return = 'please';
object = { throw: 'down', };
array = ['a', 'tapir', 'is', 'lovely', ];
string = "a tapir is 
beautiful"

Library Additions

JSON

JSON.parse and JSON.stringify are now part of the standard.

Date

Date has new methods, toISOString and toJSON It is also able to
parse ISO strings.

Object

Object gets a bunch of new methods. But they are added on Object and not
on Object.prototype.


// Creates an object with parent as prototype and properties from donor
Object.create(parent, donor);

// Meta properties of an object
var descriptor = {
  value: "test",
  writable: true,    // Can the value be changed?
  enumerable: true,  // Will it appear in for-in and Object.keys(object)?
  configurable: true, // Can the property be removed?
  set: function(value) { test = value}, // Getter
  get: function() { return test }       // Setter
}

// Methods for manipulation the descriptors
Object.defineProperty(object, property, descriptor)
Object.defineProperties(object, descriptors)
Object.getOwnPropertyDescriptor(object, property)
Object.getPrototypeOf(object)

// Returns an array of enumerable properties
Object.keys(object)
// Returns an array of all properties
Object.getOwnPropertyNames(object)

// Prevents anyone from adding properties to the object, cannot be undone.
Object.preventExtensions(object)
Object.isExtensible(object)

// Prevents anyone from changing, properties or descriptors of the object.
// The values can still be changed
Object.seal(object)
Objcect.isSealed(object)

// Prevents any changes to the object.
Object.freeze(object)
Object.isFrozen(object)

Array

Arrays are extended with a bunch of new, useful methods, leading to less
need for third-party libraries.


// Do all elements satisfy predicate?
Array.prototype.every(predicate)

// Return a new array with the elements that satisfy predicate?
Array.prototype.filter(predicate)

// Call action(element) for each element.
Array.prototype.forEach(action)

// What is the index of the first element that equals value?
Array.prototype.indexOf(value, fromIndex)

// What is the index of the last element that equal value?
Array.prototype.lastIndexOf(value, fromIndex)

// Create a new array by applying unaryFunc to each element
Array.prototype.map(unaryFunc)

// Reduces the elements of the array, by applying binaryFunc
// between the elements
// [a0, a1].reduce(+ , seed)  == seed + a0 + a1
Array.prototype.reduce(binaryFunc, seed)

// Is at least one element satisfied by the predicate?
Array.prototype.some(predicate)

String

String.prototype.trim is added to string. It trims whiespace from the
beginning and the end of a string.

Function

Function.prototype.bind(thisArg, arg, ...) is added to function. It
creates a new function by binding the value of this to a specified object.


var tapir = {
  method: function(name){
    this.name = name;
  }
};
setTimeout( tapir.method.bind(tapir, "Malayan"), 100 );

Strict Mode

A new mode called strict can be turned on by writing

function() {
  "use strict";
}

It must be the first line in the function. It could also be the first
line of the file, but that does not play well with the optimizing
practices used in Javascript, like concatenation and minification.

In strict mode:

  • Variables must be declared before usage.
  • The with statement cannot be used.
  • eval is a reserved word.
  • arguments.caller and arguments.callee cannot be used.
  • etc.

Since IE6-IE9 does not implement strict model, Crockford thinks that
they “must die” since they, “hinder the creation of useful mashups”.

This Post Has One Comment

Leave a Reply