Skip to content
On this page

class

🔗class 转 函数

js
class P3 {
  z = 10
  getA(){

  }
}

转化为 函数

js
function P3() {
  this.z = 10;
}

P3.prototype.getA = function () {
  // 实现 getA 方法的逻辑
};

let p = new P3;
p.getA()

继承

js
class P4 extends P3 {
  getB(){

  }
}
js
function P4() {
  P3.call(this); // 调用父类构造函数初始化属性
}

P4.prototype = Object.create(P3.prototype); // 设置原型链,继承 P3 的方法
P4.prototype.constructor = P4; // 修复 constructor

P4.prototype.getB = function () {
  // 实现 getB 方法的逻辑
  console.log('p4')
};

let p4 = new P4();

ts 中的 class

抽象类

ts
abstract class V {
  abstract z:number
  abstract getx():string
}

class W extends V {
  z = 10
  getx(): string {
      return ""
  }
}

接口

ts
interface A {
  z:number
  getX:(x:number)=> string;
}

class X implements A {
  z = 10
  getX (x: number) {
    return "aa"
  };
}