ES6

ES6


javascript es6 前端

1.变量/赋值 var:可以重复定义、不能限制修改、函数级作用域,没有块级作用域 let:不能重复定义、变量、块级作用域 const:不能重复定义、常量、块级作用域

解构赋值 1.左右两边必须一样,右边必须合法 2.必须定义和赋值同步完成 let [a,b,c] = [1,2,3] let {a,b,c} = [a:1,b:2,c:3]

2.函数 箭头函数 如果有且仅有一个参数,()可以省略 如果函数体只有一句话,而且是 return,{}可以省略

let show = (a) => a * 3;
alert(show(10)); //30

默认参数 (a=xx,b=xx,c=xx)

参数展开 1.接收剩余参数 function show(a,b,…args) 2.展开一个数组 […a]

3.数组/json map filter forEach reduce 汇总:一堆值返回一个值 Array.from([array-list]) => array

4.模板字符串 折行 ${}

5.面向对象

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  show() {
    alert("我叫" + this.name + " " + this.age + "岁");
  }
}
let p = new Person("a", 18);

class Worker extends Person {
  constructor(name, age, job) {
    super(name, age);
    this.job = job;
  }
  showJob() {
    alert("job" + this.job);
  }
}
let w = new Worker("a", 18, "student");
w.show();
w.showJob();

bind:给函数定死一个 this this 普通函数:根据调用者变化 箭头函数:根据所在环境 this 恒定 箭头函数比 bind 优先级更高

© 2025 Niko Xie