简体   繁体   English

当前正在运行的javascript函数名称?

[英]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.

相关问题 我可以在 JavaScript 中获取当前正在运行的函数的名称吗? - Can I get the name of the currently running function in JavaScript? Javascript元编程:获取当前执行的函数的名称,该函数已被动态重写 - Javascript metaprogramming: get name of currently executing function which was dynamically rewritten Javascript 功能目前不工作 - Javascript function not working currently 如何中断当前运行的JavaScript - How to break on currently running JavaScript 获取当前运行的QUnit测试的标题(名称) - get title (name) of currently running QUnit test 如何获取JavaScript当前运行的线路代码? - How to get the code of the line currently running in JavaScript? 获取当前运行异步 function 的结果 - Get result of currently running async function 如何在Office for Office 2013中获取当前正在运行的应用程序的名称 - How to get the name of currently running app in an App for office 2013 在当前运行 function 完成执行后将 function 排入队列执行 (setTimeout) - Enqueue function to execute after currently running function is done executing (setTimeout) 您可以使用javascript暂停WebAssembly中当前正在运行的c ++吗? - Can you use javascript to halt the currently running c++ in WebAssembly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM