有可能这样做吗:
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框架,所以如果它们中的任何一个使工作更容易,都可以使用它们。
当前回答
由于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
注意:嵌套函数不再是源元素,因此不会被提升。而且,这种技术不能用于匿名函数。
其他回答
既然你写了一个名为foo的函数,你知道它在myfile.js中,为什么你需要动态地获取这个信息?
也就是说,你可以在函数内部使用arguments.callee.toString()(这是整个函数的字符串表示),并regex出函数名的值。
下面是一个会吐出自己名字的函数:
function foo() {
re = /^function\s+([^(]+)/
alert(re.exec(arguments.callee.toString())[1]);
}
你所需要的很简单。 创建函数:
function getFuncName() {
return getFuncName.caller.name
}
在此之后,无论何时你需要,你只需使用:
function foo() {
console.log(getFuncName())
}
foo()
// Logs: "foo"
另一个用例可以是在运行时绑定事件分派器:
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()可选回调,或者在超时/间隔回调中,(即你只传递一个函数名)。
这是伊戈尔·奥斯特鲁莫夫回答的一个变体:
如果你想使用它作为参数的默认值,你需要考虑对'caller'的二级调用:
function getFunctionsNameThatCalledThisFunction()
{
return getFunctionsNameThatCalledThisFunction.caller.caller.name;
}
这将动态地允许多个函数中的可重用实现。
getFunctionsNameThatCalledThisFunction()函数 { 返回getFunctionsNameThatCalledThisFunction.caller.caller.name; } (myFunctionName = getFunctionsNameThatCalledThisFunction()) { 警报(myFunctionName); } //弹出"foo" 函数foo () { 酒吧(); } 乌鸦()函数 { 酒吧(); } foo (); 乌鸦();
如果你也想要文件名,这里是使用F-3000在另一个问题上的答案的解决方案:
function getCurrentFileName()
{
let currentFilePath = document.scripts[document.scripts.length-1].src
let fileName = currentFilePath.split('/').pop() // formatted to the OP's preference
return fileName
}
function bar(fileName = getCurrentFileName(), myFunctionName = getFunctionsNameThatCalledThisFunction())
{
alert(fileName + ' : ' + myFunctionName);
}
// or even better: "myfile.js : foo"
function foo()
{
bar();
}
这是我在这里看到的一些回应的组合。(在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/