<html>
<body>
<h2>JavaScript Class Static 方法</h2>
<p>static 方法是使用 “static” 关键字创建的,您只能在类本身上调用该方法。</p>
<p id="demo"></p>
<script>
class Car {
constructor(brand) {
this.carname = brand;
}
static hello() { // static 方法
return "Hello!!";
}
}
mycar = new Car("Tesla");
//对 Car 类调用 'hello()':
document.getElementById("demo").innerHTML = Car.hello();
//而非 'mycar' 对象:
//document.getElementById("demo").innerHTML = mycar.hello();
//此举会引发错误。
</script>
</body>
</html>