码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] 16. Object Enhancements

时间:2015-01-01 22:27:54      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

Define object


 

var color = "blue";
var speed = 120;

var car = {color, speed};

console.log(car.color);  // blue
console.log(car.speed);  // 120

 in ES5: 

var car = {color: color, speed: speed};

 

works with function


var color = "blue";
var speed = 120;
function go(){
    console.log("start");
}

var car = {color, speed, go};

console.log(car.color); //blue
console.log(car.speed); //120
car.go(); // start

 

define function inside object:


 

var color = "blue";
var speed = 120;

var car = {
    color,
    speed,
    go(){
        console.log("start");
    }
};

console.log(car.color); //blue
console.log(car.speed); //120
car.go(); // start

 

in ES5:

var car = {
    color,
    speed,
    go: function(){
        console.log("start");
    }
};

 

get computed property:


 

var color = "blue";
var speed = 120;

var car = {
    color,
    speed,
    ["go"]: function(){
        console.log("start");
    }
};

console.log(car.color); //blue
console.log(car.speed); //120
car.go(); // start

The same as:

car["go"](); // start

 

string concatenation or evaluation


 

var color = "blue";
var speed = 120;
var drive = "go";
var car = {
    color,
    speed,
    [drive]: function(){
        console.log("start");
    }
};

console.log(car.color); //blue
console.log(car.speed); //120
car.go(); // start
car["go"](); // start

 

[ES6] 16. Object Enhancements

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4197893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!