概述
- 在Es6中,class(类)作为对象的模板被引入,可以通过class关键字定义类。class的本质是function。它可以看做是一个语法糖让对象原型的写法更清晰,更向面向对象的语法
- 如果类中的属性使用static修饰是挂载在类上,可以通过类名直接调用,this也会直接指向类,如果没有使用static修饰,则需要通过new一个实列对象,实列对象调用属性或者方法,this指向还是指向实列对象
class fn {
constructor() {
console.log('构造函数');
}
static a = 1000
static fn = () => {
console.log(this.a);
console.log('这是类的方法');
}
student = 2000
teacher = 100
fn = () => {
console.log(this.student);
console.log('这是实例的方法');
}
}
const heima = new fn()
console.log(heima);
console.log(heima.student);
console.log(fn.a);
fn.fn()
heima.fn()