从函数内部获取函数的名称
Get the name of the function from inside the function
是否可以从函数内部获取函数的名称?
例:
function myFunction() {
// console.log the name of this function without knowing/specifying what the name is
// otherwise console.log('myFunction was called'); is
// more obvious (and better supported) than console.log(myFunction.name);
// console.log(arguments.callee.name); works but not in strict mode and is deprecated
}
参考:
Function.namearguments.callee
更新:
从评论和回答来看,似乎完全错过了重点。
我修改了示例以使其更清晰。
更新 2:
似乎仍然存在歧义...下面是调试过程的示例:
function one() {
console.log(arguments.callee.name + ' was called'); // one was called
// reset of the code
}
function two() {
console.log(arguments.callee.name + ' was called'); // two was called
// reset of the code
}
function three() {
console.log(arguments.callee.name + ' was called'); // three was called
// reset of the code
}
function four() {
console.log(arguments.callee.name + ' was called'); // four was called
// reset of the code
}
function five() {
console.log(arguments.callee.name + ' was called'); // five was called
// reset of the code
}
arguments.callee.name
已弃用,在严格模式下不可用。
我正在寻找一种未弃用且在严格模式下工作的方法。
试试这个...
console.log(myFunction.name);
请注意,IE尚不支持此功能,如此处所述
相关文章:
- 使用jQuery来foreach iframe-src,并通过函数获取ID和操作
- Javascript函数获取元素内容
- 从Javascript函数获取字符串
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- 从 JavaScript 函数获取 NaN 尝试多次获取相同的数据
- 从 JS 函数获取 PHP 中的值
- 如何用一个函数获取所有javascript变量
- Javascript-函数获取数组元素参数未定义/为null
- 从回调函数获取xhr状态
- 有没有一种方法可以让watch函数获取被监视文件的名称
- 从JQuery到Javascript函数获取表单对象
- 使用react.js从api函数获取json数据
- 从javascript中的递归函数获取undefined
- 使用sails.js中的beforeConnect函数获取客户端套接字
- 如何通过类名和过滤函数获取属性值
- 从php函数获取值到javascript
- 从外部函数获取参数名称
- 从方法js中的函数获取对象属性
- 使用Jquery.load()函数获取变量和页面片段
- 使用onclick函数获取点击的元素jquery