24.javascript 类

类的创建方法:记得一点要用constructor

class ClassName {
  constructor() { ... }
}

  实例

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}

  多个方法:

class ClassName {
  constructor() { ... }
  method_1() { ... }
  method_2() { ... }
  method_3() { ... }
}

  实例:

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
  age() {
    let date = new Date();
    return date.getFullYear() - this.year;
  }
}

let myCar = new Car("Ford", 2022);
document.getElementById("demo").innerHTML = "My car is " + myCar.age() + " years old.";

  

posted @ 2022-01-06 22:26  种太阳  阅读(18)  评论(0编辑  收藏  举报