Result Size: 625 x 534
x
 
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript 类方法</h2>
<p>如何定义和使用类方法。</p>
<p id="demo"></p>
<script>
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", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
</script>
</body>
</html>