ECMA Harmony and the Future of JavaScript

Posted in: javascript
I like to see language implementors (or creators in this case) talk about the design challenges or choices they're facing with in the next version of their languages. In this video Brendan Eich, the creator of JavaScript, talks about the ES 3.1/4/5 thingy and also explains what features will be added to JavaScript in some near future (at least for the ECMA standard). I'll add a couple of comments about the JS features I liked below the video.

New stuff I liked about the language

Most of the things I liked about the new features of the language are related to Meta-Programming. These new features describe new behaviors in object properties, like getters and setters, but they are also related to object and property mutability, configuration, visibility, etc. Getters and Setters Getters and Setters were implemented by B.E. more than nine years ago at mozilla, but a new syntax is introduced in the standard by adding a static function to the Object class.
Object.defineProperty(obj, "length", {
  get: function() {
    return this.computeLength();
  },
  set: function(value) {
    this.changeLength(value);
  }
});
In this example the defineProperty static method of the Object class adds the length property to the obj object. The get and set methods will be called when accessing or modifying the length property. Object.getOwnPropertyDescriptor retrieves the property descriptor of the defined property. Defining Methods and Richer Property Descriptors The Object.defineProperty method can also be used to define instance methods:
Object.defineProperty(Array.prototype, "inject", {
  value: function(memo, iterator, context) {
    iterator = iterator.bind(context);
    this.each(function(value, index) {
      memo = iterator(memo, value, index);
    });
    return memo;
  },

  configurable: false,
  enumerable: false,
  writable: false
});
The method does something similar as inject_into or reduce methods do in other languages:
[ 1, 2, 3 ].inject(0, function(a, b) { return a+b; }); //6
If configurable=true, the property will be enabled for deletion or to be changed in other ways. You can set a property to be non-enumerable by setting enumerable=false, and it won't be detected in a for in loop (or in any other "prop" in obj expression). This means that for example we could augment Object.prototype with methods without having to iterate through them in a for in loop. The writable property if setted to false won't allow you to change the value of that property. Object.create Still no support is added for classical inheritance patterns (which makes me happy I must confess). Instead, the differential inheritance pattern gets a function that had (somewhat) been implemented by frameworks like Closure, MooTools and others with the inherits and $merge functions. Object.create can be used for implementing prototypical inheritance: you can create a new class A that inherits from B by cloning an object and augmenting it with a Properties object. For example:
var Person = function() {};
Person.prototype.eat = function() {
  alert("eating");
};
//Ninja extends Person
var Ninja = function() {};
Ninja.prototype = Object.create(Person.prototype, {
  doKungFu: function() {
    alert("wootoo");
  }
});

var n = new Ninja();
n.eat(); //eating
n.doKungFu(); //wootoo
However, don't forget to instantiate the superclass in your subclass constructor:
var Point = function(x, y) {
  this.x = x;
  this.y = y;
};

Point.prototype.distanceToOrigin = function() {
  return Math.sqrt(this.x * this.x + this.y * this.y);
};
//Complex extends Point
var Complex = function(x, y) {
  Point.call(this, x, y); //call the superclass
};

Complex.prototype = Object.create(Point.prototype, {
  add: function(complex) {
    this.x += complex.x;
    this.y += complex.y;
  }
});
Object.preventExtensions, Object.seal, Object.freeze Mutability is nice but sometimes we need to make our objects immutable for design reasons, for security reasons. These methods change the mutability level of an object. I hope this was useful to you. There are a lot more interesting language features to come, so you can read the ECMA draft if you're interested in knowing more about this new version.
Comments
blog comments powered by Disqus