JavaScript --函数的作用域(全局和局部)
全局作用域
全局作用域,就算不在一个script标签也能调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var m = "aa"
</script>
<script>
console.log(m)
</script>
</body>
</html>
局部作用域
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var m = "aa"
// 定义全局函数test
function test(){
var b = "xiaoming"
console.log("test",b,m)
}
// 在外边都访问不到b
console.log(b)
</script>
<script>
// 可以调用全局变量m
console.log(m)
// 可以调用全局函数
console.log(test)
// 在外边都访问不到b
console.log(b)
test()
</script>
</body>
</html>