node. js gets the current function within the function

Source: Internet
Author: User

How does JS know within the function body which function to run inside?

To make an analogy:

function A () {    ///  I want to know here my function name A and functions a () {...}}

The call stack that uses error can match the function that is executed during the current period:

//     Gets the function that is being executed   getexecfunction ()    {new Error (). Stack.match (/at (. * ?) /g);     = Names[1].replace (' at ', '). Trim ();     return eval (name);}

Use Ex:

function A () {    console.log (getexecfunction ());} function getexecfunction () {    new Error (). Stack.match (/at (. *)/g);     = Names[1].replace (' at ', '). Trim ();     return eval (name);} A ();         // [Function:a]

If it is inside class, you can write this (code based on ES6):

class A {    //  gets the function being executed     getexecfunction () {        new Error (). Stack.match (/at a\. *?) /g);         = Names[1].replace (' at A. ', '). Trim ();         return eval ('this. ${name} ');    A () {        Console.log (this. getexecfunction ());}    } New A (). a ();    //     [function:a]

If there is any mistake, please correct me, thank you.

node. js gets the current function within the function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.