How do you find out the caller function in JavaScript when use strict is enabled?

后端 未结 5 1462
谎友^
谎友^ 2020-12-01 04:53

Is it possible to see the callee/caller of a function when use strict is enabled?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 05:45

      functionName() {
        return new Error().stack.match(/ at (\S+)/g)[1].get(/ at (.+)/);
      }
    
      // Get - extract regex
      String.prototype.get = function(pattern, defaultValue = "") {
        if(pattern.test(this)) {
          var match = this.match(pattern);
          return match[1] || match[0];
        }
        return defaultValue; // if nothing is found, the answer is known, so it's not null
      }
    

提交回复
热议问题