ECMAScript Shorthand Property Assignment Improvements

This proposal introduces new forms of shorthand assignments for both object literal initializers and object assignment patterns, allowing the use of property accessors in addition to identifiers for shorthand assignments.

Status

Stage: 0
Champion: Ron Buckton (@rbuckton)

For more information see the TC39 proposal process.

Authors

  • Ron Buckton (@rbuckton)

Proposal

Object Initializers

When property accessors are used in an object initializer, the property name of the accessor is used as the property name of the object and the value of property accessor is used as the value of the property in the object. This is illustrated by the following syntactic conversion:

const a = { o.x };

is identical in its behavior to:

const a = { x: o.x };

In addition to the dot notation for property access, bracket notation can be used as well. As with the dot notation, the expression of the bracket notation is used as the property name of the object. This is illustrated by the following syntactic conversion:

const a = { o["x"] };

is (roughly) identical in its behavior to:

const a = { ["x"]: o["x"] };

Note that rather than duplicating the expression "x" above, the actual implementation will rely on GetReferencedName.

Destructuring Assignments

When property accessors are used in a destructuring assignment, the property name of the accessor is used as the property name for the assignment property, while the property accessor itself is used as the assignment target. This is illustrated by the following syntactic conversion:

({ a.x } = o);

is identical in its behavior to:

({ x: a.x } = o);

In addition to the dot notation, bracket notation can be used here as well. When the bracket notation is used, the expression of the bracket notation is evaluated and used as the property name of the assignment property, while the property accessor itself is used as the assignment target. This is illustrated by the following syntactic conversion:

({ a["x"] } = o);

is (roughly) identical in its behavior to:

({ ["x"]: a["x"] } = o);

Note that rather than duplicating the expression "x" above, the actual implementation will rely on GetReferencedName.

Grammar

PropertyDefinition[Yield, Await]:
    MemberExpression[?Yield, ?Await] `.` IdentifierName
    MemberExpression[?Yield, ?Await] `[` Expression `]`
    CallExpression[?Yield, ?Await] `.` IdentifierName
    CallExpression[?Yield, ?Await] `[` Expression `]`
    ...

AssignmentProperty[Yield, Await]:
    MemberExpression[?Yield, ?Await] `.` IdentifierName Initializer[+In, ?Yield, ?Await]?
    MemberExpression[?Yield, ?Await] `[` Expression `]` Initializer[+In, ?Yield, ?Await]?
    CallExpression[?Yield, ?Await] `.` IdentifierName Initializer[+In, ?Yield, ?Await]?
    CallExpression[?Yield, ?Await] `[` Expression `]` Initializer[+In, ?Yield, ?Await]?

Resources

TODO

The following is a high-level list of tasks to progress through each stage of the TC39 proposal process:

Stage 1 Entrance Criteria

  • Identified a "champion" who will advance the addition.
  • Prose outlining the problem or need and the general shape of a solution.
  • Illustrative examples of usage.
  • High-level API (proposal does not introduce an API).

Stage 2 Entrance Criteria

Stage 3 Entrance Criteria

Stage 4 Entrance Criteria

  • Test262 acceptance tests have been written for mainline usage scenarios and merged.
  • Two compatible implementations which pass the acceptance tests: [1], [2].
  • A pull request has been sent to tc39/ecma262 with the integrated spec text.
  • The ECMAScript editor has signed off on the pull request.