源代碼
點擊運行
<!DOCTYPE html> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> </head> <body> <h2>JavaScript const</h2> <p> const 定義的變量併非常量,併非不可變,它定義了一個常量引用一個值。使用 const 定義的對象或者數組,其實是可變的。下面的代碼併不會報錯:</p> <p id="demo"></p> <script> // 創建常量對象 const car = {type:"Fiat", model:"500", color:"white"}; // 修改屬性: car.color = "red"; // 添加屬性 car.owner = "Johnson"; // 顯示屬性 document.getElementById("demo").innerHTML = "Car owner is " + car.owner; </script> </body> </html>
運行結果