有可能这样做吗:
myfile.js:
function foo() {
alert(<my-function-name>);
// pops-up "foo"
// or even better: "myfile.js : foo"
}
我的堆栈中有Dojo和jQuery框架,所以如果它们中的任何一个使工作更容易,都可以使用它们。
有可能这样做吗:
myfile.js:
function foo() {
alert(<my-function-name>);
// pops-up "foo"
// or even better: "myfile.js : foo"
}
我的堆栈中有Dojo和jQuery框架,所以如果它们中的任何一个使工作更容易,都可以使用它们。
当前回答
这是我在这里看到的一些回应的组合。(在FF, Chrome, IE11测试)
function functionName()
{
var myName = functionName.caller.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));
return myName;
}
function randomFunction(){
var proof = "This proves that I found the name '" + functionName() + "'";
alert(proof);
}
调用randomFunction()将提醒包含函数名的字符串。
JS小提琴演示:http://jsfiddle.net/mjgqfhbe/
其他回答
在ES5及以上版本中,无法访问该信息。
在旧版本的JS中,你可以使用arguments.callee来获取它。
不过,您可能需要解析这个名称,因为它可能包含一些额外的垃圾。不过,在某些实现中,您可以使用arguments.callee.name简单地获取名称。
解析:
function DisplayMyName()
{
var myName = arguments.callee.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));
alert(myName);
}
获取当前函数名。
这应该做到:
var fn = arguments.callee.toString().match(/function\s+([^\s\(]+)/);
alert(fn[1]);
对于调用者,只需使用caller. tostring()。
这里有一个可行的方法:
export function getFunctionCallerName (){
// gets the text between whitespace for second part of stacktrace
return (new Error()).stack.match(/at (\S+)/g)[1].slice(3);
}
然后在你们的测试中
import { expect } from 'chai';
import { getFunctionCallerName } from '../../../lib/util/functions';
describe('Testing caller name', () => {
it('should return the name of the function', () => {
function getThisName(){
return getFunctionCallerName();
}
const functionName = getThisName();
expect(functionName).to.equal('getThisName');
});
it('should work with an anonymous function', () => {
const anonymousFn = function (){
return getFunctionCallerName();
};
const functionName = anonymousFn();
expect(functionName).to.equal('anonymousFn');
});
it('should work with an anonymous function', () => {
const fnName = (function (){
return getFunctionCallerName();
})();
expect(/\/util\/functions\.js/.test(fnName)).to.eql(true);
});
});
注意,只有当测试位于/util/functions中时,第三个测试才会工作
2016年的信息是真实的。
函数声明的结果
歌剧的结果
>>> (function func11 (){
... console.log(
... 'Function name:',
... arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
... })();
...
... (function func12 (){
... console.log('Function name:', arguments.callee.name)
... })();
Function name:, func11
Function name:, func12
Chrome中的结果
(function func11 (){
console.log(
'Function name:',
arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
})();
(function func12 (){
console.log('Function name:', arguments.callee.name)
})();
Function name: func11
Function name: func12
NodeJS的结果
> (function func11 (){
... console.log(
..... 'Function name:',
..... arguments.callee.toString().match(/function\s+([_\w]+)/)[1])
... })();
Function name: func11
undefined
> (function func12 (){
... console.log('Function name:', arguments.callee.name)
... })();
Function name: func12
不能在Firefox中工作。未经IE和Edge测试。
函数表达式的结果
NodeJS的结果
> var func11 = function(){
... console.log('Function name:', arguments.callee.name)
... }; func11();
Function name: func11
Chrome中的结果
var func11 = function(){
console.log('Function name:', arguments.callee.name)
}; func11();
Function name: func11
不工作在Firefox, Opera。未经IE和Edge测试。
注:
匿名函数没有意义去检查。 测试环境
~ $ google-chrome --version
Google Chrome 53.0.2785.116
~ $ opera --version
Opera 12.16 Build 1860 for Linux x86_64.
~ $ firefox --version
Mozilla Firefox 49.0
~ $ node
node nodejs
~ $ nodejs --version
v6.8.1
~ $ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
由于arguments.callee.name是非标准的,并且在ECMAScript 5严格模式(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee)中被禁止,动态检索函数名的一个简单解决方案[像魔术变量一样]是使用作用域变量和function .name属性。
{
function foo() {
alert (a.name);
}; let a = foo
}
{
function foo2() {
alert(a.name)
}; let a = foo2
};
foo();//logs foo
foo2();//logs foo2
注意:嵌套函数不再是源元素,因此不会被提升。而且,这种技术不能用于匿名函数。