spr*_*man 172 javascript jquery dojo
是否有可能做到这一点:
myfile.js:
function foo() {
alert(<my-function-name>);
// pops-up "foo"
// or even better: "myfile.js : foo"
}
Run Code Online (Sandbox Code Playgroud)
我的堆栈中有Dojo和jQuery框架,所以如果其中任何一个更容易,它们就可用了.
Mat*_*att 187
你应该能够通过使用它来获得它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);
}
Run Code Online (Sandbox Code Playgroud)
ffo*_*orw 67
对于非匿名函数
function foo()
{
alert(arguments.callee.name)
}
Run Code Online (Sandbox Code Playgroud)
但是在错误处理程序的情况下,结果将是错误处理程序函数的名称,不是吗?
小智 37
你所需要的一切都很简单.创建功能:
function getFuncName() {
return getFuncName.caller.name
}
Run Code Online (Sandbox Code Playgroud)
在您需要之后,您只需使用:
function foo() {
console.log(getFuncName())
}
foo()
// Logs: "foo"
Run Code Online (Sandbox Code Playgroud)
And*_*y E 21
这应该这样做:
var fn = arguments.callee.toString().match(/function\s+([^\s\(]+)/);
alert(fn[1]);
Run Code Online (Sandbox Code Playgroud)
对于呼叫者,只需使用caller.toString()
.
Max*_*ber 20
getMyName
下面片段中的函数返回调用函数的名称。这是一个 hack 并且依赖于非标准功能:Error.prototype.stack
. 请注意,返回的字符串格式Error.prototype.stack
在不同引擎中的实现方式不同,因此这可能不适用于所有地方:
function getMyName() {
var e = new Error('dummy');
var stack = e.stack
.split('\n')[2]
// " at functionName ( ..." => "functionName"
.replace(/^\s+at\s+(.+?)\s.+/g, '$1' );
return stack
}
function foo(){
return getMyName()
}
function bar() {
return foo()
}
console.log(bar())
Run Code Online (Sandbox Code Playgroud)
关于其他解决方案:arguments.callee
在严格模式下是不允许的,Function.prototype.caller
是非标准的,在严格模式下是不允许的。
Jam*_*ard 10
这必须属于"世界上最丑陋的黑客"的范畴,但是你走了.
首先,打印当前函数的名称(如在其他答案中)似乎对我有限,因为你已经知道函数是什么了!
但是,找出调用函数的名称对跟踪函数非常有用.这是一个正则表达式,但使用indexOf将快3倍:
function getFunctionName() {
var re = /function (.*?)\(/
var s = getFunctionName.caller.toString();
var m = re.exec( s )
return m[1];
}
function me() {
console.log( getFunctionName() );
}
me();
Run Code Online (Sandbox Code Playgroud)
这是一种可行的方式:
export function getFunctionCallerName (){
// gets the text between whitespace for second part of stacktrace
return (new Error()).stack.match(/at (\S+)/g)[1].slice(3);
}
Run Code Online (Sandbox Code Playgroud)
然后在你的测试中:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
请注意,第三个测试仅在测试位于/ util/functions中时才有效
参数对象是所有非箭头函数中可用的局部变量。
您可以通过使用函数的参数对象来引用该函数内部的函数参数。
它具有调用函数所用的每个参数的条目,第一个条目的索引为 0。
所以你基本上可以arguments.callee.name
在命名函数中使用 but ,如下所示:
function i_have_a_name() {
console.log(`My name is:`, arguments.callee.name)
}
Run Code Online (Sandbox Code Playgroud)
> i_have_a_name()
My name is: i_have_a_name
Run Code Online (Sandbox Code Playgroud)
不幸的是它在箭头函数中不可用:
const i_have_a_name = () => {
console.log(`My name is:`, arguments.callee.name)
}
Run Code Online (Sandbox Code Playgroud)
> i_have_a_name()
Uncaught ReferenceError: arguments is not defined
at i_have_a_name (REPL3:2:32)
Run Code Online (Sandbox Code Playgroud)
来源:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
自从提出这个问题以来,当前函数的名称及其获取方式在过去 10 年中似乎发生了变化。
现在,我不是一个了解所有浏览器历史的专业 Web 开发人员,以下是它在 2019 年 chrome 浏览器中的工作原理:
function callerName() {
return callerName.caller.name;
}
function foo() {
let myname = callerName();
// do something with it...
}
Run Code Online (Sandbox Code Playgroud)
其他一些答案遇到了一些关于严格的 javascript 代码和诸如此类的 chrome 错误。
归档时间: |
|
查看次数: |
112637 次 |
最近记录: |