W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript Class Static 方法</h1> <p>static 方法是使用 "static" 关键字创建的,您只能在类本身上调用该方法。</p> <p id="demo"></p> <script> class Car { constructor(name) { this.name = name; } static hello() { return "Hello!!"; } } let myCar = new Car("Ford"); //You can call 'hello()' on the Car Class: document.getElementById("demo").innerHTML = Car.hello(); // But NOT on a Car Object: // document.getElementById("demo").innerHTML = myCar.hello(); // this will raise an error. </script> </body> </html>