开发者社区> 问答> 正文

js 中 callee 与 caller 的作用

js 中 callee 与 caller 的作用

展开
收起
茶什i 2019-11-22 14:15:36 881 0
1 条回答
写回答
取消 提交回答
  • caller 返回一个调用当前函数的引用 如果是由顶层调用的话 则返回 null (举个栗子哈 caller 给你打电话的人 谁给你打电话了 谁调用了你 很显然是下面 a 函数的执行 只有在打电话的时候你才能知道打电话的人是谁 所以对于函数来说 只有 caller 在函数执行的时候才存在)

    var callerTest = function() {
      console.log(callerTest.caller);
    };
    function a() {
      callerTest();
    }
    a(); //输出function a() {callerTest();}
    callerTest(); //输出null
    
    

    callee 返回一个正在被执行函数的引用 (这里常用来递归匿名函数本身 但是在严格模式下不可行)

    callee 是 arguments 对象的一个成员 表示对函数对象本身的引用 它有个 length 属性(代表形参的长度)

    var c = function(x, y) {
      console.log(arguments.length, arguments.callee.length, arguments.callee);
    };
    c(1, 2, 3); //输出3 2 function(x,y) {console.log(arguments.length,arguments.callee.length,arguments.callee)}
    
    2019-11-22 14:16:37
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Let's Work Together On The Future Of JavaScript Through TC39 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载