类的创建方法:记得一点要用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.";