Skip to the content.

ES6 常用新特性

  1. const let
  2. symbol
  3. 解构: const {key1, key2} = obj;
  4. 扩展运算符 ...:
const obj2 = { ...obj1, newKey: "" };
const arr2 = [...arr1, "test"];
// 扩展运算符和解构赋值结合使用
const [first, ...rest] = [1, 2, 3, 4, 5];
// first = 1
// rest = [2,3,4,4,5]
//扩展运算符还可以将字符串转为真正的数组
[..."hello"]; //['h', 'e', 'l', 'l', 'o']
//替换ES5中用apply方法为函数绑定参数

function f(x, y, z) {}
es5: f.apply(null, [1, 2, 3]);
es6: f(...[1, 2, 3]);
  1. 对象属性简写: const obj = {name, age}
  2. promise: 异步函数
  3. generator, 返回迭代器对象,yeild 分割步伐
  4. 箭头函数()=> {};不改变 this 指向
  5. async await : 像编写同步代码一样编写异步逻辑
  6. 函数的默认参数,给函数传递默认值,避免在函数里面写参数的判断逻辑
  7. 参数剩余参数:
function (arg1, ...others) {}
  1. 数组赋值
const [firstName, lastName] = ["jock", "chen"];
  1. ?. 对象中是否包含某个属性,避免没有属性报错。返回undefined.
  2. ?? 区别于||,如果前面的值为null或’undefined’,就把后面的值赋值给前面的变量。
const arg = undefined ?? "init";
  1. 模板字符串

返回首页