在 JavaScript 中未声明变量时会发生什么?
可以的。在全局作用域下,即使不声明变量也可以使用。下面的“no var”变量“points”将查找作用域链,var 关键字并不适用 −
<html>
<body>
<script>
var rank = 5;
points = 50;
marks = 300;
// Anonymous function
(function() {
points = 100; //overwrites global scope points
var rank = 4; //new rank variable is created in this' function's scope
var marks = 900;
document.write(rank+"\r
"); //prints 4
document.write(points+"\r
"); //prints 100
document.write(marks+"\r
"); //prints 900
})();
document.write('<br/>');
document.write('<br/>');
document.write(rank+"\r
"); //prints 5
document.write(points+"\r
"); //prints 100
document.write(marks+"\r
"); //prints 300
</script>
</body>
</html>广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP