有可能这样做吗:
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框架,所以如果它们中的任何一个使工作更容易,都可以使用它们。
当前回答
既然你写了一个名为foo的函数,你知道它在myfile.js中,为什么你需要动态地获取这个信息?
也就是说,你可以在函数内部使用arguments.callee.toString()(这是整个函数的字符串表示),并regex出函数名的值。
下面是一个会吐出自己名字的函数:
function foo() {
re = /^function\s+([^(]+)/
alert(re.exec(arguments.callee.toString())[1]);
}
其他回答
arguments对象是一个局部变量,可在所有非箭头函数中使用。 可以使用函数的arguments对象来引用函数内部的参数。 它对调用函数的每个参数都有条目,第一个条目的索引为0。
所以你基本上可以使用arguments。callee。name但是在命名函数内部,像这样:
function i_have_a_name() {
console.log(`My name is:`, arguments.callee.name)
}
> i_have_a_name()
My name is: i_have_a_name
不幸的是,它不能在箭头函数中使用:
const i_have_a_name = () => {
console.log(`My name is:`, arguments.callee.name)
}
> i_have_a_name()
Uncaught ReferenceError: arguments is not defined
at i_have_a_name (REPL3:2:32)
来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
由于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
注意:嵌套函数不再是源元素,因此不会被提升。而且,这种技术不能用于匿名函数。
另一个用例可以是在运行时绑定事件分派器:
MyClass = function () {
this.events = {};
// Fire up an event (most probably from inside an instance method)
this.OnFirstRun();
// Fire up other event (most probably from inside an instance method)
this.OnLastRun();
}
MyClass.prototype.dispatchEvents = function () {
var EventStack=this.events[GetFunctionName()], i=EventStack.length-1;
do EventStack[i]();
while (i--);
}
MyClass.prototype.setEvent = function (event, callback) {
this.events[event] = [];
this.events[event].push(callback);
this["On"+event] = this.dispatchEvents;
}
MyObject = new MyClass();
MyObject.setEvent ("FirstRun", somecallback);
MyObject.setEvent ("FirstRun", someothercallback);
MyObject.setEvent ("LastRun", yetanothercallback);
这里的优点是dispatcher可以很容易地重用,并且不需要接收dispatcher队列作为参数,相反,它与调用名称一起隐含…
最后,这里给出的一般情况是“使用函数名作为参数,这样你就不必显式地传递它”,这在很多情况下可能是有用的,比如jquery animate()可选回调,或者在超时/间隔回调中,(即你只传递一个函数名)。
这里有一个可行的方法:
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中时,第三个测试才会工作
Try:
alert(arguments.callee.toString());