W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h2>JavaScript 类继承</h2> <p>使用 “extends” 关键字从另一个类继承所有方法。</p> <p>使用 “super” 方法调用父级的构造函数。</p> <p id="demo"></p> <script> class Car { constructor(brand) { this.carname = brand; } present() { return 'I have a ' + this.carname; } } class Model extends Car { constructor(brand, mod) { super(brand); this.model = mod; } show() { return this.present() + ', it is a ' + this.model; } } mycar = new Model("Tesla", "Model3"); document.getElementById("demo").innerHTML = mycar.show(); </script> </body> </html>