tri_inn

tri_inn

  • NA
  • 1.2k
  • 233.5k

Javascript What is constructor function

Jun 29 2017 9:27 AM
tell me when js function will be called as constructor function ?
 
if we write a simple js function then it will be called as constructor function ?
 
  1. function myFunction(a, b) {  
  2.     return a * b;                  
  3. }  
  4.   
  5. var x = myFunction(4, 3);         
 so tell me myFunction() will be consider as constructor function ? if no then describe the reason.
 
tell me why below js function is consider as constructor function ?
  1. function person(first, last, age, eye) {  
  2.     this.firstName = first;  
  3.     this.lastName = last;  
  4.     this.age = age;  
  5.     this.eyeColor = eye;  
  6. }  
  7. var myFather = new person("John""Doe", 50, "blue");  
  8. var myMother = new person("Sally""Rally", 48, "green");  
 when new keyword will be used to call js function then it will be consider as constructor function ?
 
please guide me. thanks
 

Answers (2)