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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM