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.";

  

上一篇:003-Java注解-@Deprecated


下一篇:javascript中数据类型判断的几种方法