How to get function name in strict mode [proper way]

落花浮王杯 提交于 2019-12-01 06:58:50

问题


arguments.callee unfortunatelly deprecated, and using it throws an error in "strict mode".

Is there any new proper(standard) alternative for getting function name inside actual function? Or will it be in future plans ECMA6, 7?

Recent answer is no more than dirty hack and not acceptable for me answer.

And arguments.callee.caller.name not working either (nodejs v7.5.0)


回答1:


Is there any new proper (standard) alternative for getting function name inside actual function?

No, there is not.

Or will it be in future plans for ES?

No, given that there is no need for it. Inside the current function, you know the name and could just as well use a string literal, in other functions you just need some reference (but not .callee).




回答2:


If you use Node.js there is a useful package exactly for this problem: caller-id

var callerId = require('caller-id');

function foo() {
    bar();
}

function bar() {
    var callerName = callerId.getData().functionName; /* 'foo' */
}


来源:https://stackoverflow.com/questions/42473446/how-to-get-function-name-in-strict-mode-proper-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!