JavaScript之arguments

来源:互联网 发布:什么是访客网络设置 编辑:程序博客网 时间:2024/06/07 14:51

arguments

arguments[n]
arguments是个类似数组但不是数组的对象,能够由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length。arguments对象存储的是实际传递给函数的参数,而不局限于函数声明所定义的参数列表。用Array.prototype.slice.call()可将arguements对象转变为数组

function add(n){    var res=[].slice.call(arguments);    console.log(arguments.length);    console.log(res);}add(5);//1//[5]

callee

arguments.callee
返回当前函数。表示对函数对象本身的引用,这有利于匿名函数的递归或确保函数的封装性。

function My(){    console.log(arguments.callee);}My();//function My(){//  console.log(arguments.callee);//}function add(n){    if(n<1) return 0;    return n+arguments.callee(n-1);}console.log(add(5));//15function add(n){    console.log(arguments.length);//实际传给函数的参数列表长度    console.log(arguments.callee.length);//函数声明所定义的参数列表长度}add();//0//1

caller

FunctionName.caller
返回调用当前函数的函数。若函数是在顶层调用的,返回null。

function My(){    console.log(My.caller.toString());    //也可用console.log(arguments.callee.caller.toString());}function Your(){    My();}Your();//function Your(){//  My();//}function My2(){    console.log(arguments.callee.caller);}My2();//null
1 0