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