Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//Enhanced object literals
//変数名と同じキーや無名関数のfunctionを省略できる
var name = 'chuck', age = '24';
//ES5
var person = {
name : name,
age : age,
hi : function() {return 'hi';}
};
//ES6
var person = {
name, age, hi() {return 'hi';}
};
//keyに動的な値を使用できる。
var rand = Math.random();
//ES5
var obj = {};
obj[rand] = 'value';
//ES6
var obj = {[rand]: 'value'};
console.log(obj);
// Object {0.2180616082623601: "value"}
var obj2 = {[Math.ramdom()]: 'value'};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment