[英]Currently running javascript function name?
This works fine AFAIK: 这可以正常使用AFAIK:
(function f() {
console.log(f.name); //logs f
})();
But some of the answers posted here are a lot longer, which makes me think that I might be missing a gotcha (In other words it works in this case, BUT ...) with the above statement? 但是, 这里发布的一些答案要长得多,这使我认为我可能会丢失上述语句的陷阱(换句话说,在这种情况下,它可以工作,但...)?
Here's a slightly different typescript variation: 这是一个稍微不同的打字稿变体:
function f1() {}
function f2(f:Function) {
console.log(f.name);
}
f2(f1);
The Function.name
property is only available in ES6/ES2015-compliant engines. Function.name
属性仅在符合ES6 / ES2015的引擎中可用。 So for example if you try to access it without additional configuration in Typescript you will get the error: 因此,例如,如果您尝试在Typescript中不进行其他配置的情况下访问它,则会收到错误消息:
[ts] Property 'name' does not exist on type 'Function'.
[ts]类型“功能”上不存在属性“名称”。
So for typescript include es2015
in your --lib value to get the property declaration. 因此,对于打字稿,
es2015
在--lib值中包含es2015
以获取属性声明。
{
"compilerOptions": {
...
"lib": ["es2015"], /* Specify library files to be included in the compilation. */
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.