Stage 2 Draft / September 29, 2016

Object Rest/Spread Properties

Introduction#

Rest properties collect the remaining field names that are not already picked off by the destructuring pattern. Those keys and their values are copied onto a new object.

1: let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
2: x; // 1
3: y; // 2
4: z; // { a: 3, b: 4 }

Spread properties in object initializers copies field names and their values from a provided object onto the newly created object.

1: let n = { x, y, ...z };
2: n; // { x: 1, y: 2, a: 3, b: 4 }

1Spread Properties#

Syntax

PropertyDefinition[Yield]:IdentifierReference[?Yield] CoverInitializedName[?Yield] PropertyName[?Yield]:AssignmentExpression[In, ?Yield] MethodDefinition[?Yield] ...AssignmentExpression[In, ?Yield]

1.1Runtime Semantics: PropertyDefinitionEvaluation#

With parameters object and enumerable.

PropertyDefinition:...AssignmentExpression
  1. Let exprValue be the result of evaluating AssignmentExpression.
  2. Let fromValue be GetValue(exprValue).
  3. ReturnIfAbrupt(fromValue).
  4. Let excludedNames be a new empty List.
  5. Return CopyDataProperties(object, fromValue, excludedNames).

2Rest Properties#

Syntax

ObjectAssignmentPattern[Yield]:{AssignmentRestProperty[?Yield]opt} {AssignmentPropertyList[?Yield]} {AssignmentPropertyList[?Yield],AssignmentRestProperty[?Yield]opt} AssignmentRestProperty[Yield]:...DestructuringAssignmentTarget[?Yield] ObjectBindingPattern[Yield]:{BindingRestProperty[?Yield]opt} {BindingPropertyList[?Yield]} {BindingPropertyList[?Yield],BindingRestProperty[?Yield]opt} BindingRestProperty[Yield]:...BindingIdentifier[?Yield] ...BindingPattern[?Yield]

2.1Runtime Semantics: DestructuringAssignmentEvaluation#

With parameter value.

ObjectAssignmentPattern:{AssignmentPropertyList} {AssignmentPropertyList,}
  1. Perform ? RequireObjectCoercible(value).
  2. Perform ? PropertyDestructuringAssignmentEvaluation for AssignmentPropertyList using value as the argument.
  3. Return NormalCompletion(empty).
ObjectAssignmentPattern:{AssignmentRestProperty}
  1. Let excludedNames be a new empty List.
  2. Return the result of performing RestDestructuringAssignmentEvaluation of AssignmentRestProperty with value and excludedNames as the arguments.
ObjectAssignmentPattern:{AssignmentPropertyList,AssignmentRestProperty}
  1. Let excludedNames be the result of performing ? PropertyDestructuringAssignmentEvaluation for AssignmentPropertyList using value as the argument.
  2. Return the result of performing RestDestructuringAssignmentEvaluation of AssignmentRestProperty with value and excludedNames as the arguments.
NoteThe following productions have moved to PropertyDestructuringAssignmentEvaluation to keep the return type of DestructuringAssignmentEvaluation consistent. AssignmentPropertyList:AssignmentPropertyList,AssignmentProperty AssignmentProperty:IdentifierReferenceInitializeropt AssignmentProperty:PropertyName:AssignmentElement

2.2Runtime Semantics: PropertyDestructuringAssignmentEvaluation#

With parameters value.

NoteThese collect a list of all destructured property names rather than just empty completion. AssignmentPropertyList:AssignmentPropertyList,AssignmentProperty
  1. Let status propertyNames be the result of performing PropertyDestructuringAssignmentEvaluation for AssignmentPropertyList using value as the argument.
  2. ReturnIfAbrupt(status propertyNames).
  3. Return Let nextNames be the result of performing PropertyDestructuringAssignmentEvaluation for AssignmentProperty using value as the argument.
  4. ReturnIfAbrupt(nextNames).
  5. Append each item in nextNames to the end of propertyNames.
  6. Return propertyNames.
AssignmentProperty:IdentifierReferenceInitializeropt
  1. Let P be StringValue of IdentifierReference.
  2. Let lref be ? ResolveBinding(P).
  3. Let v be ? GetV(value, P).
  4. If Initializeropt is present and v is undefined, then
    1. Let defaultValue be the result of evaluating Initializer.
    2. Let v be ? GetValue(defaultValue).
    3. If IsAnonymousFunctionDefinition(Initializer) is true, then
      1. Let hasNameProperty be ? HasOwnProperty(v, "name").
      2. If hasNameProperty is false, perform SetFunctionName(v, P).
  5. Return Perform ? PutValue(lref, v).
  6. Return a new List containing P.
AssignmentProperty:PropertyName:AssignmentElement
  1. Let name be the result of evaluating PropertyName.
  2. ReturnIfAbrupt(name).
  3. Return Let status be the result of performing KeyedDestructuringAssignmentEvaluation of AssignmentElement with value and name as the arguments.
  4. ReturnIfAbrupt(status).
  5. Return a new List containing name.

2.3Runtime Semantics: RestDestructuringAssignmentEvaluation#

With parameters value and excludedNames.

AssignmentRestProperty[Yield]:...DestructuringAssignmentTarget
  1. If DestructuringAssignmentTarget is neither an ObjectLiteral nor an ArrayLiteral, then
    1. Let lref be the result of evaluating DestructuringAssignmentTarget.
    2. ReturnIfAbrupt(lref).
  2. Let restObj be ObjectCreate(%ObjectPrototype%).
  3. Let assignStatus be CopyDataProperties(restObj, value, excludedNames).
  4. ReturnIfAbrupt(assignStatus).
  5. If DestructuringAssignmentTarget is neither an ObjectLiteral nor an ArrayLiteral, then
    1. Return PutValue(lref, restObj).
  6. Let nestedAssignmentPattern be the parse of the source text corresponding to DestructuringAssignmentTarget using either AssignmentPattern or AssignmentPattern[Yield] as the goal symbol depending upon whether this AssignmentRestProperty has the [Yield] parameter.
  7. Return the result of performing DestructuringAssignmentEvaluation of nestedAssignmentPattern with restObj as the argument.

2.4Runtime Semantics: BindingInitialization#

With parameters value and environment.

ObjectBindingPattern:{BindingPropertyList} {BindingPropertyList,}
  1. Let excludedNames be the result of performing PropertyBindingInitialization for BindingPropertyList using value and environment as the argument.
  2. ReturnIfAbrupt(excludedNames).
  3. Return NormalCompletion(empty).
ObjectBindingPattern:{BindingRestProperty}
  1. Let excludedNames be a new empty List.
  2. Return the result of performing RestBindingInitialization of BindingRestProperty with value, environment and excludedNames as the arguments.
ObjectBindingPattern:{BindingPropertyList,BindingRestProperty}
  1. Let excludedNames be the result of performing PropertyBindingInitialization of BindingPropertyList using value and environment as arguments.
  2. ReturnIfAbrupt(excludedNames).
  3. Return the result of performing RestBindingInitialization of BindingRestProperty with value, environment and excludedNames as the arguments.
NoteThe following productions have moved to PropertyBindingInitialization to keep the return type of BindingInitialization consistent. BindingPropertyList:BindingPropertyList,BindingProperty BindingProperty:SingleNameBinding BindingProperty:PropertyName:BindingElement

2.5Runtime Semantics: PropertyBindingInitialization#

With parameters value and environment.

NoteThese collect a list of all bound property names rather than just empty completion. BindingPropertyList:BindingPropertyList,BindingProperty
  1. Let status boundNames be the result of performing PropertyBindingInitialization for BindingPropertyList using value and environment as arguments.
  2. ReturnIfAbrupt(status boundNames).
  3. Return Let nextNames be the result of performing PropertyBindingInitialization for BindingProperty using value and environment as arguments.
  4. ReturnIfAbrupt(nextNames).
  5. Append each item in nextNames to the end of boundNames.
  6. Return boundNames.
BindingProperty:SingleNameBinding
  1. Let name be the string that is the only element of BoundNames of SingleNameBinding.
  2. Return Let status be the result of performing KeyedBindingInitialization for SingleNameBinding using value, environment, and name as the arguments.
  3. ReturnIfAbrupt(status).
  4. Return a new List containing name.
BindingProperty:PropertyName:BindingElement
  1. Let P be the result of evaluating PropertyName.
  2. ReturnIfAbrupt(P).
  3. Return Let status be the result of performing KeyedBindingInitialization of BindingElement with value, environment, and P as the arguments.
  4. ReturnIfAbrupt(status).
  5. Return a new List containing P.

2.6Runtime Semantics: RestBindingInitialization#

With parameters value, environment and excludedNames.

BindingRestProperty:...BindingIdentifier
  1. Let restObj be ObjectCreate(%ObjectPrototype%).
  2. Let assignStatus be CopyDataProperties(restObj, value, excludedNames).
  3. ReturnIfAbrupt(assignStatus).
  4. Let bindingId be StringValue of BindingIdentifier.
  5. Let lhs be ResolveBinding(bindingId, environment).
  6. ReturnIfAbrupt(lhs).
  7. If environment is undefined, return PutValue(lhs, restObj).
  8. Return InitializeReferencedBinding(lhs, restObj).
BindingRestProperty:...BindingPattern
  1. Let restObj be ObjectCreate(%ObjectPrototype%).
  2. Let assignStatus be CopyDataProperties(restObj, value, excludedNames).
  3. ReturnIfAbrupt(assignStatus).
  4. Return the result of performing BindingInitialization of BindingPattern with restObj and environment as the arguments.

3Abstract Operations#

3.1CopyDataProperties (target, source, excluded)#

Note 1These spec proposals refer to the abstract operation CopyDataProperties(target, source, excluded) which follow similar semantics as the ES2015 Object.assign(target, ...sources) with the addition of the excluded argument which is a list of property names to be excluded. It also uses the CreateDataProperty operation instead of Set. Note 2The target passed is in here is always a newly created object which doesn't leak in case of an error being thrown. It's not observable when the actual object construction and initialization happens nor whether it is interleaved with reading the properties off the original object.

When the abstract operation CopyDataProperties is called with arguments target, source and excluded, the following steps are taken:

  1. Assert: Type(target) is Object.
  2. If source is undefined or null, let keys be a new empty List.
  3. Else,
    1. Let from be ToObject(source).
    2. Let keys be ? from.[[OwnPropertyKeys]]().
  4. Repeat for each element nextKey of keys in List order,
    1. If nextKey is not contained in excluded, then
      1. Let desc be ? from.[[GetOwnProperty]](nextKey).
      2. If desc is not undefined and desc.[[Enumerable]] is true, then
        1. Let propValue be ? Get(from, nextKey).
        2. Perform ? CreateDataProperty(target, nextKey, propValue).
  5. Return target.

ACopyright & Software License#

Copyright Notice

© 2016 Sebastian Markbåge

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.