转载至大佬吴迪98的文章ES5/6新特性_阮一峰es5-CSDN博客
ES5
1. 严格模式
<script>
"use strict";
var aa = 1;
</script>
支持严格模式的浏览器:
Internet Explorer 10 +、Firefox 4+、 Chrome 13+、 Safari 5.1+、 Opera 12+
严格模式下:
- 变量必须先声名后赋值
- 不允许直接使用以0开头的八进制表示,如果要表示八进制数字,以 0o 为前缀
- 如果直接使用函数名调用函数,则函数体中的this为undefined。
ES6
1. 箭头函数(lambda表达式)
箭头函数的 this 会一直往上一层找,直到找到 this 对象
<script>
const aa = () => {
console.log(this)
console.log(1)
}
aa();
</script>
2. 解构赋值
<script>
//对象解构
const a = {name:'hhh', age: 1}
console.log(a)
//age 和 name 会自动映射对象属性名的值
const{age, name} = a
//等效于
//const age = a.age
//const name = a.name
console.log('age=', age, 'name=', name)//age= 1 name= hhh
//数组解构
const arr = ['hhh', 1]
const [param1, param2] = arr
console.log(param1,param2) // hhh 1
</script>
3. ...
//...组装数组
const arr1 = ['a', 'b', 'c']
const arr2 = [...arr1 , 'd', 'e', 'f']
console.log(arr2) // ['a', 'b', 'c', , 'd', 'e', 'f']
//拆分数组
const arr3 = ['a', 'b', 'c', 'd', 'e', 'f']
const [a, ...b] = arr3
console.log(a) // a
console.log(b) // ['b', 'c', 'd', 'e', 'f']
4. Promise
5. Import 和 Export
// 引入部分
import dva from 'dva';
// 引入部分
import { connect } from 'dva';
import { Link, Route } from 'dva/router';
// 引入全部并作为 github 对象
import * as github from './services/github';
// 导出默认
export default App
// 部分导出,需 import { App } from './file';引入
export class App extend Component {};
6. 新增 Map 和 Set
6.1 Map
set(key, val) |
get(key) |
delete(key) |
clear() |
entries() |
has() |
keys() |
values() |
6.2 Set
和 Map 差不多