exe*_*ook 16 javascript strict
我需要当前函数名作为字符串来登录我们的日志工具.但arguments.callee.name
只能在松散模式下工作.如何获取功能名称"use strict"
?
geo*_*org 31
为了记录/调试目的,您可以Error
在记录器中创建一个新对象并检查其.stack
属性,例如
function logIt(message) {
var stack = new Error().stack,
caller = stack.split('\n')[2].trim();
console.log(caller + ":" + message);
}
function a(b) {
b()
}
a(function xyz() {
logIt('hello');
});
Run Code Online (Sandbox Code Playgroud)
您可以将函数绑定为其上下文,然后您可以通过this.name
属性访问其名称:
function x(){
console.log(this.name);
}
x.bind(x)();
Run Code Online (Sandbox Code Playgroud)