<!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>函数--参数</title>
</head>
<body>
<script>
/*
javascript函数--参数
*/
/*
1.形参、实参
2.arguments
3.arguments对象运用
*/
// 1.形参、实参
// 略
// 2.arguments
/*
arguments 是一个对应于传递给函数的参数的类数组对象。
arguments只有函数内部使用,
可以通过索引访问参数,如果不存在则会返回undefined
由实参决定,不会因形参个数变化。
*/
function func1(a, b) {
//索引获取
console.log(arguments[0]);// expected output: 1
console.log(arguments[1]);// expected output: 2
console.log(arguments[2]);// expected output: 3
//数量
console.log(arguments.length)// expected output: 3
}
func1(1, 2, 3);
// 2.1arguments.callee属性
/*
argument.callee表示当前正在执行的函数,在比较时是严格相等的。
*/
function foo(){
//console.log(arguments.callee);//true
console.log(arguments.callee==foo);//flase
console.log(arguments.callee===foo);//true
}
foo();
/*
通过arguments.callee属性获取到函数对象后,可以直接传递参数重新进行函数的调用,
这个属性常用于匿名函数的递归调用。
*/
function create(){
return function(n){
if(n <= 1)
return 1;
return n * arguments.callee( n - 1 );
};
}
var result=create()(5);
console.log(result);//120
// 3.arguments对象运用
//3.1实参个数判断
/*
判断参数个数是否满足,或者类型
*/
function f(x,y,z){
//检查参数个数
if(arguments.length !==3){
throw new Error('参数个数少应为三个!实际个数为'+arguments.length)//Uncaught Error: 参数个数少应为三个!实际个数为2
}
}
// f(1,2)
//3.2任意个数的参数处理
// 略
//3.3模拟函数重载
// 略
</script>
</body>
</html>