创建对象
案例
继承
注意点
- 在 ES6 中类没有变量提升,所以必须先定义类,才能通过类实例化对象
- 类里面的共有属性和方法一定要加 this 使用
- 类里面的 this 指向问题:constructor 里面的 this 指向实例对象, 方法里面的 this 指向这个方法的调用者
let that;class Star {constructor (uname, age) {that = this;this.uname = uname;this.age = age;// btn按钮调用sing方法this.btn = document.querySelector("button");this.btn.onclick = this.sing;// constructor 里面的this 指向的是 创建的实例对象console.log("constructor: ", this);}sing() {// 这个sing方法里面的 this 指向的是 btn 这个按钮,因为这个按钮调用了这个函数console.log("sing:", this); // buttonconsole.log(that.uname); // that里面存储的是constructor里面的this}dance() {// 这个dance里面的this 指向的是实例对象 ldh 因为ldh 调用了这个函数console.log("dance:", this);}}let rick = new Star("Rick", 20);rick.dance();
