ES6常用语法
let、const
💡 Tips:不可重复声明,const不可改,块级作用域限制
javascript
let name1 = 'zhangsan';
const AGE = 10;
模板字符串
javascript
const info = `my name is ${name1}, i am ${AGE}`;
属性简写、方法简写
javascript
const obj1 = {
name1,
run() {
console.log('i can run !');
},
};
箭头函数
javascript
const arr = [1, 2, 3, 4];
arr.forEach((item, i) => {
console.log(`arr索引为:${i}, 值为:${item}`);
});
promise
💡 Tips:解决回调地狱,异步请求
javascript
const p = new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('succeed');
} else {
reject('failed');
}
}, 3000);
});
p.then(res => {
console.log('promise success --', res);
}).catch(err => {
console.log('promise fail --', err);
});
async、await
💡 Tips:对 promise 的优化写法,更清晰
javascript
function main() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('main 内部方法');
resolve('main 执行成功');
}, 3000);
});
}
async function getMain() {
const data = await main();
console.log('getMain 方法执行', data);
}
getMain();
console.log('外部方法');
解构赋值
javascript
let { name, age } = {
name: 'zhangsan',
age: 12,
};
let [arr1, arr2] = [2, 3];
import、export
javascript
import { mapMutations } from 'vuex';
export const fn = (data) => {};
扩展运算符
💡 Tips:对象合并
javascript
const obj = {
name: 'zhangsan',
age: 19,
};
const obj3 = {
...obj,
age: 10,
};