源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p> 如果将值赋给未声明的变量,它将自动成为全局变量: </p> <p id="demo"></p> <script> myFunction(); // code here can use carName as a global variable document.getElementById("demo").innerHTML = "我能显示 " + carName; function myFunction() { carName = "Volvo"; } </script> </body> </html>
运行结果