Strawman: Enhanced Object Initialisers

Allen Wirfs-Brock, Microsoft August 10, 2009

Abstraction creation is a central theme of object-based programming and ECMAScript provides many mechanisms that support patterns for creating object based abstractions. However, most of these patterns are constructive in nature using procedural code to create the abstractions. This approach is powerful in that it allows a wide variety of different technical mechanism to be used to construct various abstractions. However, this variety can also be problematic creating complexity for both readers and writers of ECMAScript program and making it difficult to ECMAScript implementation to recognize common abstraction patterns so they can be optimized. Most other programming language solve these issues by provide a straightforward declarative mechanism for defining object abstractions.

ECMAScript does provided a basic declarative mechanism for defining object-based abstractions. Object literals provide a declarative mechanism for defining objects that in most situations is more concise, readable, and maintainable than programmatic object creation using constructor functions and dynamic property insertion. The declarative nature of object initialisers also makes it easier for implementations to perform various object representation optimization. However, existing ECMAScript object initialisers do provide declarative solutions for a number of abstraction capabilities that are common used with ECMAScript objects.

This strawman explores ways in which ECMAScript object initialisers could be extended to make them more useful for building complete abstractions. A number of individual candidate extensions are identified that could selectively and individually be added to the language. However, the individual extensions in combination turn ECMAScript object initialisers into a declarative abstraction mechanism that is powerful enough to serve as the primary abstraction mechanism of the language.

The goal of these extensions is not to create a full featured class abstraction similar to those found in popular class-based languages. Instead, it attempts to incrementally improve the existing ECMAScript abstraction mechanisms without introducing anything that a typical user might perceive as new fundamental concepts. As such these extensions may be viewed either as an alternative to the classes proposal or as an adjunct to it.

Individual Extensions

Combined Syntax

The following provides an integrated syntax definition for all of the individual extensions combined with the ES5 Object initialiser syntax:

ConstructorDeclaration :
constructor Identifier ( FormalParameterListopt ) ObjectLiteral

ObjectLiteral :
{ }
{ MetaPropertiesopt PrivatePartopt PropertyNameAndValueList }
{ MetaPropertiesopt PrivatePartopt PropertyNameAndValueList , }

MetaProperties :
[ MetaPropertyList ]

PrivatePart :
{ FunctionBody }

MetaPropertyList :
MetaProperty
MetaPropertyList , MetaProperty

MetaProperty :
parent : AssignmentExpression
sealed
frozen
extensible

PropertyNameAndValueList :
PropertyAssignment
PropertyNameAndValueList , PropertyAssignment

PropertyAssignment :
PropertyName : AssignmentExpression
const PropertyName : AssignmentExpression
var PropertyName : AssignmentExpression
constopt get PropertyName ( ) { FunctionBody }
varopt set PropertyName ( PropertySetParameterList ) { FunctionBody }
method PropertyName ( FormalParameterListopt ) { FunctionBody }

PropertyName :
IdentifierName
StringLiteral
NumericLiteral

PropertySetParameterList :
Identifier

 
strawman/object_initialiser_extensions.txt · Last modified: 2010/05/26 21:59 by brendan
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki