繁体   English   中英

JavaScript:获取自身内部的函数名称

[英]JavaScript: Get function name inside of itself

是的,我知道有很多关于这个问题的线程,对于初学者:

从函数本身获取函数名称[重复]

从内部获取函数名称

不推荐使用Arguments.callee - 应该使用什么?

但是给出答案的问题是arguments.callee已被弃用 所有答案都说只是给你的功能一个名字。 但是,据我所知,这并不能解决我的问题。 说我有以下功能:

function blah() {
  // arguments.callee.name is deprecated
  console.log('The function name is: ' + arguments.callee.name + '.');
}

但是因为它被弃用了,我不应该使用它,所以我应该使用什么呢? 有什么办法可以在函数本身内部访问函数名,或者我只是出于概率吗?

如果它让事情变得更容易,我使用的是Ext JS框架,但我还没有找到一种了解函数名称的方法。 如果没有,是否有jQuery方法? 我在这里绝望。

您可以激发异常并检查堆栈跟踪。

以下上下文证明适用于Chrome浏览器:

function test () {
  try { [].undef () } catch (e) {
     console.log (e.stack.split ('\n')[1].split (/\s+/)[2]);
  }
}

有关更强大的实现,请参阅http://www.eriwen.com/javascript/js-stack-trace/ ,它可在任何浏览器中提供完整的堆栈跟踪。

更现代,更全面的堆栈跟踪分析器是http://stacktracejs.com

随着一些探索,我想出了这个SO线程 ,所以建立在其上,我做了一个非常hacky解决方案工作(在Chrome和FF ...不确定IE,但我怀疑它的工作原理)。 警告 :这是我自己使用的非常具体的,所以你的里程肯定会有所不同。 无论如何,这是我的代码:

getLogLocation: function() {
  var ua = navigator.userAgent;
  var isFF = ua.search(/firefox/i) !== -1 ? true : false;
  var isChrome = ua.search(/chrome/i) !== -1 ? true : false;
  if (isFF || isChrome) {
    var stack = Error().stack,
        cname = '',
        funcPattern,
        classPattern = /.*\/(.*)\.js/;  // looking for something between the last backslash and .js
    if (stack) {
      var stacks = stack.split('\n');
      if (stacks) {
        var theStack;
        // the browsers create the stack string differently
        if (isChrome) {
          // the stack has getClassName, then logMessage, then our calling class, but Chrome has some added garbage
          theStack = stacks[4];
          funcPattern = /.*\.(.*)\s+\(/;   // looking for something between a period and the first paren
        }
        else {
          theStack = stacks[2];
          funcPattern = /^\.*(.*)\@/;  // looking for something between a period and an @ symbol
        }
        var matches = theStack.match(classPattern);
        cname = matches[1] + '::';
        matches = theStack.match(funcPattern);
        cname += matches[1] + ':';
      }
    }
    return cname;
  }
}

如果你对我的堆栈看起来很好奇,这里是相关的:

Firefox (切出很多行)

".getClassName@http://127.0.0.1/javascripts/app/mixins/ConsoleMixin.js?_dc=1383836090216:72
.logMessage@http://127.0.0.1/javascripts/app/mixins/ConsoleMixin.js?_dc=1383836090216:31
.constructor@http://127.0.0.1/javascripts/app/BaseController.js?_dc=1383836089659:39
..."

Chrome (前2行是我必须容纳的垃圾......之后,它类似于FF的Stack字符串)

"Error
    at Error (<anonymous>)
    at Ext.define.getLogLocation (http://127.0.0.1/javascripts/app/mixins/ConsoleMixin.js?_dc=1383836606405:72:19)
    at Ext.define.logMessage (http://127.0.0.1/javascripts/app/mixins/ConsoleMixin.js?_dc=1383836606405:31:24)
    at new Ext.define.constructor (http://127.0.0.1/javascripts/app/BaseController.js?_dc=1383836606265:39:14)
    ..."

看到这个jsFiddle的工作示例......必须更改堆栈值,因为我们不再使用Ext JS了。

现在,一点解释。 getLogLocation作为Ext JS类( ConsoleMixin )中的函数驻留, ConsoleMixin另一个函数( logMessage )调用getLogLocation,logMessage由我们的外部类的函数( constructor函数)调用,这就是为什么我必须补偿前2个堆栈值。 就像我说的,非常hacky和具体到我的需要,但希望有人可以利用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM