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

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

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

5条回答
  •  伪装坚强ぢ
    2020-12-01 05:53

    It does not worked for me Here is what I finally do, just in case it helps someone

    function callerName() {
      try {
        throw new Error();
      }
      catch (e) {
        try {
          return e.stack.split('at ')[3].split(' ')[0];
        } catch (e) {
          return '';
        }
      }
    
    }
    function currentFunction(){
      let whoCallMe = callerName();
      console.log(whoCallMe);
    }
    

提交回复
热议问题