01 函数中的arguments
1.1 传多的参数也会存在里面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function foo(name, age) {
console.log(arguments[0])
console.log(arguments[1])
console.log(arguments[2])
}
foo("kobe", 12, "洛杉矶")
</script>
</body>
</html>
1.2 遍历的方式获取参数
<!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>
function foo(name, age){
for (var i = 0; i < arguments.length; i++){
console.log(arguments[i]);
}
}
foo("tom", 12)
</script>
</body>
</html>
02 作用域
2.1 无块级作用域举例
<!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 message = "hello world"
console.log("代码块中访问:", message);
}
console.log("代码块外面访问:", message);
</script>
</body>
</html>
结果如下
2.2 函数有自己作用域举例
<!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>
function foo(){
var message = "hello world"
console.log("函数内部访问message", message);
}
foo()
console.log("函数外部访问message", message);
</script>
</body>
</html>
结果如下
03 全局变量和局部变量的理解
3.1 全局变量
<!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>
// 在全局(script元素中)定义一个变量,这个变量可以在定义之后的任何范围访问得到,那么这个变量就是全局变量
var message = "hello world"
function foo(){
console.log("foo中访问message", message);
function bar(){
console.log("bar中访问message", message);
}
bar()
}
foo()
</script>
</body>
</html>
3.2 局部变量
定义在函数内部,只有在函数内部才能进行访问,称之为局部变量
<!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 message = "hello world"
function foo(){
// 在函数内部去访问函数之外的变量,访问的变量称之为外部变量
console.log("foo中访问message", message);
var name = "kobe"
function bar(){
// name对于bar函数来说就是外部变量
console.log("bar中访问name",name);
// message对于bar函数来说也是外部变量
console.log("bar中访问message", message);
}
bar()
}
foo()
</script>
</body>
</html>
04 变量的查找顺序
05 函数的表达式
06 JS函数特点
6.1 函数赋值给一个变量
<!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 foo = function() {
return 3
}
console.log(foo())
</script>
</body>
</html>
6.2 函数在变量之间来回传递
<!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 foo = function() {
return 3
}
var bar = foo
console.log(bar())
</script>
</body>
</html>
6.3 函数作为另外一个函数的参数
<!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 foo1 = function(){
console.log("foo1函数被执行了~~~~~~");
}
function bar(fn){
fn()
}
bar(foo1)
</script>
</body>
</html>
6.4 函数作为另外一个函数的返回值
<!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>
function foo(name){
function bar(){
console.log(name);
}
return bar
}
console.log(foo("tom")());
</script>
</body>
</html>
6.5 函数存储在一种数据结构中
<!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 obj = {
name: "tom",
eating: function(){
console.log("eating");
}
}
obj.eating()
</script>
</body>
</html>
07 回调函数的理解
7.1 概念理解
<!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>
function foo(fn){
// 通过fn去调用bar函数,这个过程就是函数的回调
fn()
}
function bar(){
console.log("bar函数执行了");
}
foo(bar)
</script>
</body>
</html>
7.2 回调函数应用场景
上面的写法可以优化,如下