如果 JavaScript 中没有声明变量会发生什么情况?
javascriptweb developmentfront end technology
是的,这是可以做到的。当您拥有全局作用域时,无需声明变量即可使用变量。以下"无 var"变量"points"将查看作用域链,wince var 关键字不使用"minus"
<html> <body> <script> var rank = 5; points = 50; marks = 300; // 匿名函数 (function() { points = 100; //覆盖全局范围 points var rank = 4; //在此函数范围内创建新的 rank 变量 var marks = 900; document.write(rank+"\r
"); //打印 4 document.write(points+"\r
"); //打印 100 document.write(marks+"\r
"); //打印 900 })(); document.write('<br/>'); document.write('<br/>'); document.write(rank+"\r
"); //打印 5 document.write(points+"\r
"); //打印 100 document.write(marks+"\r
"); //打印 300 </script> </body> </html>