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