<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 词法作用域:
// 在js中只有函数能够形成一个作用域, 所以, 词法作用域也叫做函数作用域
//
// 在js中, 每一个函数都形成了一个作用域,
// 所以, 在函数内部声明的变量,在函数的外部是访问不到的
// 分析作用域的面试题的时候, 一般:
// 1 将所有的变量或者是函数,能提升的全部都提升
// 2 再分析
// 面试题 1:
// function foo() {
// var num = 123;
// console.log(num); // 123
// }
// foo();
// console.log(num); // num is not defined
// 2
/*var scope = "global";
function foo() {
console.log(scope); // undefined
var scope = "local";
console.log(scope); // local
}
foo();*/
// 提升完之后的结构:
var scope;
function foo() {
var scope;
console.log(scope); // undefined
scope = "local";
console.log(scope); // local
}
scope = "global";
foo();
</script>
</body>
</html>