跳至內容

JavaScript/Class

維基教科書,自由的教學讀本
class Point {
  #x;
  #y;
  publicFieldName = 1;
  #privateFieldName = 2;

  constructor(x, y) {
    this.#x = x;
    this.#y = y;
  }

  equals(point) {
    return this.#x === point.#x && this.#y === point.#y;
  }

  #method() { //私有方法
    // ...
  }
}

a=new Point(1,2);
b=new Point(1,2);
console.log(a==b);
console.log(a.equals(b));

class Foo {
  #fieldName = 1;
}

class Bar extends Foo { // 类继承
  fieldName = 2; // 没有问题
}