2
let text = 'how are you';
let foo = {
type : 'foo',
text
};
console.log(foo);
爲什麼鍵text
是自動分配的,有沒有這樣的語法的參考?Javascript的道具分配沒有鍵?
let text = 'how are you';
let foo = {
type : 'foo',
text
};
console.log(foo);
爲什麼鍵text
是自動分配的,有沒有這樣的語法的參考?Javascript的道具分配沒有鍵?
這是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 };
[速記屬性名稱(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Property_definitions) – Teemu