InterviewSolution
| 1. |
What are constructor functions in JavaScript? |
|
Answer» In JavaScript everything is an Object. So whenever we create an FUNCTION, there is a one object which is created for that function. But actually there is another object which is created which is known as the Prototype object. When we create an instance of the function with the NEW keyword interesting THINGS happens. It create something known as __proto__. This is created by the JavaScript engine for every function call using the new keyword. Now, let’s LOOK how to create an function using prototype and the benefit of it. The below code have two function haveFun and drinkBreak. The function haveFun is an normal function inside the Constructor function. The function drinkBreak is created outside and added to the Prototype Object using it’s reference prototype. Both the function seems to be doing the same thing, then what’s the benefit. |
|