執行 ❯
建立您
自己的
網站
×
更改方向
儲存程式碼
更改主題,深色/淺色
前往 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>