zoukankan      html  css  js  c++  java
  • js函数 test.caller 谁在调用test函数

    返回调用指定函数的函数.

    
     function test() {
          if (test.caller === null)
            console.log('test 函数在全局调用');
    
          // 获取调用 test函数, 的函数名
          console.log(test.caller.name );
          // 更上面一样
          console.log( arguments.callee.caller.name );
    
          // 获取 test函数的auguments
          console.log( Array.prototype.slice.call(arguments));
          // 获取 调用test函数,的函数的 arguments
          console.log( Array.prototype.slice.call(arguments.callee.caller.arguments));
        }
    
        function a(arg1, arg2) {
          test(1)
        }
    
        function b() {
          test(2)
        }
        a(123)
        b()
    
        function test2 (n) {
          if(n <=0){
            return null
          }
          // 判断 函数是否递归
          console.log(
            test2.caller &&
            test2.caller.name === 'test2'
              ? '递归'
              : test2.caller && test2.caller.name
          );
          return test2(n - 1)
        }
    
        test2(3)
    
  • 相关阅读:
    pku2351 Colored Sticks
    JSOI2010 满汉全席
    享元模式
    适配器模式
    合成模式
    原型模式
    创建型设计模式
    建造者模式
    装饰模式
    单例模式
  • 原文地址:https://www.cnblogs.com/ajanuw/p/8084893.html
Copyright © 2011-2022 走看看