Strawman: Declarative Object and Class Abstractions Based Upon Extended Object Initialisers

This proposal has been supercede by Intergrated Object Literal Extensions.

Allen Wirfs-Brock Original Proposal August 10, 2009
Revised Proposal March 2011

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. It also allows for programmer defined abstractions with application specific construction semantics. However, this variety can also be problematic. It creates complexity for both readers and writers of ECMAScript program and making it difficult to ECMAScript implementations to recognize common abstraction patterns so they can be optimized. Most other programming language solve these issues by providing a straightforward declarative mechanism for defining object abstractions based upon a standardize semantics.

ECMAScript does provided a basic declarative mechanism for defining object-based abstractions. Object initialisers 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 not provide declarative solutions for a number of abstraction capabilities that are common used with ECMAScript objects.

This strawman proposed ways in which ECMAScript object initialisers can be extended to make them more useful for building complete object abstractions. A number of individual candidate extensions are identified. These extensions could be selectively and individually 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 introduce a new semantics of objects that differs from what is already in ECMAScript. Instead, it attempts to incrementally improve the existing ECMAScript abstraction mechanisms without introducing anything that a typical user might perceive as new fundamental concepts. The proposal does introduce a more concrete manifestation of “classes”, but the semantics they exhibit are exactly those that are already used by the built-in ECMAScript library objects.

Individual Extensions

The following describes how the Private Names extension integrates with extended Object Initialisers.

Instance Variables can also be used with extended Object and Class Initialisers.

Combined Syntax

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

ObjectLiteral :
{ Blockopt }
{ MetaProperties }
{ MetaProperties , Blockopt }
{ MetaProperties , PrivateNamesListopt PropertyNameAndValueList }
{ MetaProperties , PrivateNamesListopt PropertyNameAndValueList , Blockopt }
{ PrivateNamesListopt PropertyNameAndValueList }
{ PrivateNamesListopt PropertyNameAndValueList , Blockopt }

ArrayLiteral :
[ Ellisionopt ]
[ MetaProperties Ellisionopt ]
[ MetaProperties , ElementList ]
[ MetaProperties , ElementList , Ellisionopt ]
[ ElementList ]
[ ElementList , Ellisionopt ]

MetaProperties :
< MetaPropertyList >

MetaPropertyList :
MetaProperty
MetaPropertyList , MetaProperty

MetaProperty :
proto : MemberExpression
sealed
frozen
closed

PrivateNamesList :
PrivateName
PrivateNamesList PrivateName

PrivateName :
private identifier ,
private identifier : AssignmentExpression ,

PropertyNameAndValueList :
PropertyAssignment
PropertyNameAndValueList , PropertyAssignment

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

PropertyName :
IdentifierName
StringLiteral
NumericLiteral

PropertySetParameterList :
Identifier

ClassDeclaration :
class Identifier ClassBody

ClassExpression :
class Identifieropt ClassBody

ClassBody
{ }
{ ClassMetaProperties }
{ ClassMetaProperties , }
{ ClassMetaProperties , PrivateNamesListopt ClassPropertyNameAndValueList }
{ ClassMetaProperties , PrivateNamesListopt ClassPropertyNameAndValueList , }
{ PrivateNamesListopt ClassPropertyNameAndValueList }
{ PrivateNamesListopt ClassPropertyNameAndValueList , }

ClassMetaProperties :
< ClassMetaPropertyList >

ClassMetaPropertyList :
ClassMetaProperty
ClassMetaPropertyList , MetaProperty

ClassMetaProperty :
proto : AssignmentExpression
superclass : AssignmentExpression
sealed
frozen
closed

ClassPropertyNameAndValueList :
ClassPropertyAssignment
ClassPropertyNameAndValueList , ClassPropertyAssignment

ClassPropertyNameAndValueList :
ClassPropertyAssignment
ClassPropertyNameAndValueList , ClassPropertyAssignment

ClassPropertyAssignment :
class PropertyAssignment
ConstrutorBody
PropertyAssignment

ConstrutorBody :
new ObjectLiteral
new ( FormalParameterListopt ) ObjectLiteral

 
strawman/object_initialiser_extensions.txt · Last modified: 2011/05/20 21:56 by allen
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki