Strawman: Object Literal Extensions

Axel Rauschmayer axel at rauschma.de
Fri May 20 17:54:38 PDT 2011


> I've update [1] and integrated with some related proposals to make the integrated proposal tree starting at http://wiki.ecmascript.org/doku.php?id=strawman:basic_object_literal_extensions 
> 
> We should use that as the starting point for the class integration.


Very nice. This combo-proposal helps tremendously with subtyping. See example below.

Remaining pain points:

- Having to hard-code the super-type in the sub-constructor.
  => Would super.constructor(name) work (in the example below)?

- Needing two steps for defining a type. A single construct for doing so would be nice. With the proposal, not much is needed, though. Just some very light syntactic sugar: a way to attach a constructor function to an object literal so that the latter is assigned to the prototype property of the former.

========== New style ==========

// Supertype
function Person(name) {
    this.name = name;
}
Person.prototype = {
    describe() {
        return "Person called "+this.name;
    }
}

// Subtype
function Worker(name, title) {
    Person.call(this, name);
    this.title = title;
}
Worker.prototype = Person.prototype <| {
    describe() {
        return super.describe(this)+" ("+this.title+")";
    }
};

========== Old style ==========

// Supertype
function Person(name) {
    this.name = name;
}
Person.prototype = {};
Person.prototype.describe = function() {
    return "Person called "+this.name;
};

// Subtype
function Worker(name, title) {
    Person.call(this, name);
    this.title = title;
}
Worker.prototype = Object.create(Person.prototype);
Worker.prototype.describe = function() {
    return Person.prototype.describe.call(this)+" ("+this.title+")";
};

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110521/3e47f5a6/attachment-0001.html>


More information about the es-discuss mailing list