How to get the function name from within that function using JavaScript ?
Given a function and the task is to get the name of the function from inside the function using JavaScript.
- JavaScript String substr() Method: This method gets parts of a string, starting at the character at the defined position, and returns the specified number of characters.
Syntax:
string.substr(start, length)
- Parameters:
- start: This parameter is required. It specifies the position from where to begin the extraction. The first character is at index 0. If start is positive and greater than or equal to the length of the string, this method returns an empty string. If start is negative, this method uses it as an index from the end. If start is negative or larger than the length of the string, start is used as 0.
- length: This parameter is optional. It specifies the number of characters to extract. If not used, it extracts the whole string.
- Function prototype name property: This is a Function object’s read-only name property denoting the function’s name as defined when it was designed, or “anonymous” when created anonymously.
Syntax:
func.name
- Return value: It returns the name of the function.
Example 1: This example first converts the function to string using toString() method and then extracts the name from that string using substr() method.
html
<!DOCTYPE HTML> < html > < head > < title > How to get the function name from within that function </ title > </ head > < body style = "text-align:center;"> < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </ p > < button onclick = "GFG_click()"> Get Name </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = GFGFunction; function functionName(fun) { var val = fun.toString(); val = val.substr('function '.length); val = val.substr(0, val.indexOf('(')); el_down.innerHTML = val; } function GFGFunction() { } function GFG_click() { functionName(GFGFunction); } </ script > </ body > </ html > |
Output:
- Before clicking on the button:
- After clicking on the button:
Example 2: This example gets the name of the function by using function proptotype name property.
html
<!DOCTYPE HTML> < html > < head > < title > How to get the function name from within that function </ title > </ head > < body style = "text-align:center;"> < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </ p > < button onclick = "GFG_click()"> Get Name </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = GFGFunction; function functionName(fun) { var val = fun.name; el_down.innerHTML = val; } function GFGFunction() { } function GFG_click() { functionName(GFGFunction); } </ script > </ body > </ html > |
Output:
- Before clicking on the button:
- After clicking on the button:
Please Login to comment...