var that;
class Star{
constructor(name,age){
that = this;
this.name= name;
this.age=age;
// this.sing();
this.btn = document.querySelector('button');
this.btn.onclick = this.sing;
}
sing(){
console.log(that.name);//that里面存储的constructor中的this
}
}
var ldh = new Star("刘德华")
1.ES6中类没有变量提示所以一定要 先定义类,然后再实例化对象
2.类里面的共有属性和方法一定要加this使用
3.constructor里面的this指向实例对象,方法中的this指向这个方法的调用者
网友评论