JS中的caller和callee
186 字
1 分钟
JS中的caller和callee
caller
- 检验函数被谁调用过
- 检验函数被谁调用过 返回值是调用这个函数的函数本身,没有的话返回null
arguments.callee
- 函数本身
function Fn() { console.log(arguments.callee.caller);//-->function fn() {Fn()} //-->arguments.callee 函数本身 //caller:检验函数被谁调用过 返回值是调用这个函数的函数本身,没有的话返回null console.log(Fn.prototype.constructor === arguments.callee);//-->true}function fn() { Fn()}fn();function sum(n) { console.log(arguments.callee===arguments.callee.caller); //->第一次输出false,因为第一次是函数自己执行,即自调用 自调用的时候console.log(arguments.callee.caller) 输出null 而此时arguments.callee 函数本身仍然是sun函数本身 所以输出false; // -->当自调用执行完成后 arguments.callee===arguments.callee.caller===sum这个函数本身 ---> 返回true if(n<=0){ return 0 } return n+sum(--n);}console.log(sum(10));//55支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
JS中的caller和callee
https://gaodaxiu-blog.netlify.app/posts/JS中的caller和callee/











