arguments是什么?
答:是1个对象 是一个长的很很像数组的对象
arguments内容是什么?
答:是函数运行时的实参列表
(function (d,e,f){
console.log(arguments); //object 其实在JS中,除了基本类型以外都叫对象,但是数组还是有和对象不一样的特点
console.log(arguments[0]); //hello
console.log(arguments[2]); //!
console.log(arguments[3]); //haha
arguments[0]='china';
console.log(d); // china 由此可以看到 形参和对应的arguments单元是相互影响的--即一个值
})('hello','world','!','haha') //arguments收集了‘所有’的实参,即使没有与之相对应的形参
在以上该函数内,无法用d,e,f形参来取得‘haha’ 因为‘haha’没有与之对应的形参
但是我们可以用arguments来获取任意多个的实参。
如何获取函数运行时,真正收到的实参个数?(在多态/里可以用到)
(function (d,e,f) {
console.log(arguments);
console.log(arguments.length);
})('hello','world','!','haha')
/*arguments.callee 属性代表’当前运行的函数‘
*/
(function (d,e,f){
console.log(arguments.callee);
})('hello','world','!','haha')
function t(n) {
if(n==1){
return 1;
}else{
return n+t(n-1);
}
}
alert(t(100));
//==================================================
//接下来我们不用函数名,用 匿名函数立即执行 并且还完成递归
//使用arguments.callee就可以实现 这样匿名函数实现了递归
alert(
(function (n){
if(n<=1){
return 1;
}else {
return n + arguments.callee(n - 1);
}
})(100)
);
// =====================================================
/*
再思考:如果是匿名函数,又不允许用callee 也要完成递归,怎么办? 提示 Y算子
函数运行内部关键的3个对象:
AO:本函数AO没有某个属性,则继续去外层函数的AO上找 直到全局对象window 因此这个链叫做作用于链
arguments:每个函数有自己的callee 但不往外层函数接着找arguments的相关属性 即不形成链
this:
*/