es6-类
类的声明:
class People { constructor() { }
类的成员属性和方法:
class People { //成员属性,一般定义在构造函数上 constructor(name, height) { this.name = name; this.height = height; this.intro(); } //成员方法 intro() { console.log(this.tpl()); } } export { People };
类的继承:
class Student extends People { //继续People类 }
使用父类的方法:
class Student extends People { //继续People类 constructor(name) { super(name); //super使用父类constructor super.fn()//调用父类fn方法 } }
给元素绑定事件
class People {
//成员属性
constructor(id) {
name='小红';
//这里的this指向的是创建实例的对象-People
this.btn = document.querySelector(id)
this.btn.onclick = this.intro
}
//成员方法-方法中的this指向调用者
intro() {
console.log(this.name)//这里的this指向调用者btn
}
dance() {
console.log(this)//这里的this指向调用者a,a=People
}
}
const a = new People()
a.dance()
new People('#btn')

浙公网安备 33010602011771号