How to detect if a function is called as constructor?
The problem is to identify where the function call is a constructor call or not.
Approach 1:
- Use the instanceof property.
- If the current instance is the instance of function then this is a constructor call.
- Else, this is a general function call.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to detect if a function is called as constructor in JavaScript ? </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_Run()"> Click here </button> <p id="GFG_DOWN" style= "font-size: 23px; font-weight: bold; color: green; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to check if the " + "function is called as a constructor<br>" + "Function name - GFG_FUN2"; function GFG_FUN2(val) { var temp = false; if (this instanceof GFG_FUN2 && !this.__previouslyConstructedByVal) { temp = true; this.__previouslyConstructedByVal = true; } return temp; } function gfg_Run() { // Function call var temp = GFG_FUN2(); if (temp == true) { el_down.innerHTML = "Function is called as constructor."; } else { el_down.innerHTML = "Function is not called as constructor."; } } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Approach 2:
- Use the .constructor property.
- If this.constructor is equal to the function name then this is a constructor call.
- Else, this is a general function call.
Example: This example illustrate the approach discussed above.
<!DOCTYPE HTML> <html> <head> <title> Detect function is called as constructor </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN" style="font-size: 23px; font-weight: bold; color: green; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to check if the " + "function is called as a constructor<br>" + "Function name - GFG_FUN2"; function GFG_FUN2(val) { var temp = false; if (this.constructor == GFG_FUN2) { temp = true; } if (temp == true) { el_down.innerHTML = "Function is called as constructor."; } else { el_down.innerHTML = "Function is not called as constructor."; } } function gfg_Run() { new GFG_FUN2(); //Call to the function. } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- Detect a device is iOS or not using JavaScript
- How to detect a mobile device in jQuery?
- How to detect when user scrolls to the bottom of a div ?
- How to detect the user's device using jQuery ?
- How to detect flash is installed or not using JavaScript ?
- How to detect escape key press using jQuery?
- How to detect browser or tab closing in JavaScript ?
- HTML | DOM comment() constructor
- JavaScript | RegExp constructor
- Detect the Operating System of User using JavaScript
- JQuery | Detect a textbox content is changed or not
- How to detect touch screen device using JavaScript?
- JavaScript | Number constructor Property
- JavaScript | Date constructor Property
- JavaScript | String constructor Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



