运行 ❯
获取您
的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Classes uses "strict mode"</h1> <p>In a JavaScript Class you cannot use variable without declaring it.</p> <p id="demo"></p> <script> class Car { constructor(name, year) { this.name = name; this.year = year; } age() { // date = new Date(); // This will not work const date = new Date(); // This will work return date.getFullYear() - this.year; } } const myCar = new Car("Ford", 2014); document.getElementById("demo").innerHTML = "My car is " + myCar.age() + " years old."; </script> </body> </html>