ES6的对象属性简写

在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");    
});

猜你喜欢

转载自www.cnblogs.com/smile-fanyin/p/10895864.html