2016-09-22 19 views
2
let text = 'how are you'; 
let foo = { 
    type : 'foo', 
    text 
}; 
console.log(foo); 

爲什麼鍵text是自動分配的,有沒有這樣的語法的參考?Javascript的道具分配沒有鍵?

+5

[速記屬性名稱(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Property_definitions) – Teemu

回答

4

這是ECMAScript 2015(ES6)對象初始值設定項中的shorter notation

//ES5 
var a = "foo", 
    b = 42, 
    c = {}; 

var o = { 
    a: a, 
    b: b, 
    c: c 
}; 

//ES6 shorter notation available to achieve the same: 
var o = { a, b, c };