Strawman: Object Literal Extensions

# Axel Rauschmayer (14 years ago)

I've update [1] and integrated with some related proposals to make the integrated proposal tree starting at 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+")"; };