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

ES6的对象属性简写

时间:2019-05-20 19:13:57      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:div   The   function   strong   pre   OLE   用法   hello   直接   

在ES6中允许我们在设置一个对象的属性的时候不指定属性名。

不使用ES6:

const name=‘Ming‘, age=‘18‘, city=‘Shanghai‘;

const student ={
    name:name,
    age:age,
    city:city
};
console.log(student);

 

使用ES6:

const name=‘Ming‘, age=‘18‘, city=‘Shanghai‘;

const student ={
    name,
    age,
    city
};
console.log(student);

对象中直接写变量,非常简洁。

 

 

Promise 是异步编程的一种解决方案,比传统的解决方案callback更加的优雅。它最早由社区提出和实现的,ES6 将其写进了语言标准,统一了用法,原生提供了Promise对象。

不使用ES6:

嵌套两个setTimeout回调函数:

setTimeout(function(){
    console.log(‘Hello‘);
 
    setTimeout(function(){
        console.log(‘Hi‘);    
    },1000);
},1000);

使用ES6:

var waitSecond =new Promise(function(resolve, reject){
    setTimeout(resolve, 1000);
});

waitSecond.then(function(){
    console.log("Hello");
    return waitSecond;    
}).then(function(){
        console.log("Hi");    
});

 

ES6的对象属性简写

标签:div   The   function   strong   pre   OLE   用法   hello   直接   

原文地址:https://www.cnblogs.com/smile-fanyin/p/10895864.html

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