1.let可以取代var
let 声明的变量存在作用域
const 声明的变量为常量
2.静态的字符串建议使用''或者动态的字符串建议使用
3.变量解构
const arr = [1, 2, 3, 4];
// bad
const first = arr[0];
const second = arr[1];
// good
const [first, second] = arr;
// bad
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
}
// good
function getFullName(obj) {
const { firstName, lastName } = obj;
}
// best
function getFullName({ firstName, lastName }) {
}
4.对象尽量静态化,一旦定义,就不得随意添加新的属性。如果添加属性不可避免,要使用Object.assign方法
this.searchparams = Object.assign({}, this.searchparams, {
page: this.pagination.current,
limit: this.pagination.pageSize
});
5.使用扩展运算符(...)拷贝数组。
// good
const itemsCopy = [...items];
兼容性:
https://kangax.github.io/compat-table/es6/
ES6:
1.变量
let:不能重复声明;变量 可以修改 块级作用域
const:不能重复声明 常量 不能修改 块级作用域
2.函数
参数扩展:
function(a,b,...args){
}
这种写法调用函数传参数 前两个对应ab 后面无论传几个都通过...args解析
但是...args只能放最后。
数组展开:
const arr = [1,2,3];
...arr ===1, 2, 3
let [a,b,c] = [1,2,3]---->a=1;b=2,c=3;
3.数组
3.1map
let arr = [33,55,77,88];
arr.map(item=>{alert(item);})
3.2reduce
arr.reduce((temp,item,index)=>{})
3.3filter
arr.filter(item=>{})
foreach
4.字符串
4.1新增方法
startsWith
endsWith
4.2.拼接字符串
${}
5.面向对象
6.Promise
7.generator
8.模块化
Array
1.Array.flat:
flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。
2.new Set(arr)对数组进行去重
网友评论