原生js底层基础(十三)JavaScript auguments.callee、caller

mac2024-04-05  57

1.arguments.callee

arguments.callee 代表的是函数自身的引用,在哪个函数里调用就是哪个函数的引用 使用场景:有的时候使用匿名函数,没有办法使用函数名调用,可以选择arguments.callee 例如,在写求阶乘函数(递归)时:

var num = (function(n){ if(n <= 1){ return 1; } return n * arguments.callee(n-1); }(20))

当有嵌套函数时,遵循在哪个函数里指向的就是哪个函数的引用

function test(){ console.log(arguments.callee); function demo (){ console.log(arguments.callee);//输出demo的引用 } demo(); } test();//输出test的引用

2.caller

caller表示调用函数的环境,实际开发用的比较少,但是常常跟callee一起作为面试的知识点。 function test(){ demo(); } function demo(){ console.log(demo.caller) } test() 输出结果

function test(){ demo(); }
最新回复(0)