Fa parte di ECMAScript 2015 (o ECMAScript 6). È possibile creare nuove proprietà in Oggetti in letterali Object, semplicemente specificando gli identificatori.
Citando MDN's Object Initializer's Property Definitions section,
Con ECMAScript 6, v'è una notazione più breve disponibile per ottenere lo stesso:
var a = "foo",
b = 42,
c = {};
// Shorthand property names (ES6)
var o = { a, b, c };
La sezione corrispondente ECMAScript specifica 6 è here,
AssignmentProperty: IdentifierReferenceInitializer opt
- Let P essere StringValue di IdentifierReference.
- Lasciare lref be ResolveBinding (P).
- ReturnIfAbrupt (P).
- Let v essere GetV (valore, P).
- ReturnIfAbrupt (v).
- Se Initializer opt è presente e v è indefinito, quindi
- Let defaultValue essere il risultato della valutazione Initializer.
- Lasciate v be GetValue (defaultValue).
- ReturnIfAbrupt (v).
- Se IsAnonymousFunctionDefinition (Initializer) è vero, quindi
- Let hasNameProperty essere hasOwnProperty (v, "nome").
- ReturnIfAbrupt (hasNameProperty).
- Se hasNameProperty è falso, eseguire SetFunctionName (v, P).
- Return PutValue (Lref, v).
In sostanza, la specifica dice che, se si utilizza solo un identificatore, verrà creata una nuova proprietà con il nome del identificatore, e il valore sarà il valore effettivo di tale identificatore. Può anche essere un nome della funzione.
var a = "foo", b = 42, c = {}, d = function() {};
console.log({a, b, c, d});
// { a: 'foo', b: 42, c: {}, d: [Function] }
Versione 43.0.2357.81 Costruito su Ubuntu 14.04, eseguito su LinuxMint 17.1 (64-bit) – exebook