1. 定义类
class A {//=> class 声明一个类,只能通过 new 执行construtor() {//=> 给自身添加私有属性和方法// 只有放入 this 中的才会出现在实例上this.a = 12;this.b = 13;}// 这是 ES6 对象中函数的简写getName () {//=> 给原型添加方法console.log(1);}name = 'a' //=> 给原型添加属性// ES7 现在浏览器是不支持getY=()=>{// 这是一个箭头函数在原型上的写法}static getName() {//=> 相当于把 A 当作对象来处理,添加方法console.log(2);}// ES7 静态属性static name = 'aa'}
通过 class 定义的类,只能使用 new 来创建,不能像函数一样直接执行。
可以在创建类的时候,传递参数,此时在 constructor 里面进行接收
class A {construtor(name) {this.name = name;}}new A('a');
2. extend 继承
class A extends Fn{constructor(){// 继承了类Fn的私有属性// 不仅继承私有,还继承公有super();}}
