Object constructor :- JavaScript


  • When you create a object for the function, the function called automatically.
  • In this example we have created a function name is student, created the object var stu = new student("Merbin", "Joe") for that function, so that function invoke automatically.
Code:-

  1. <!DOCTYPE html> 
  2. <html> 
  3.  
  4. <head> 
  5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  6.  
  7.  <script> 
  8.   /*When you create the object the constructor call automatically.*/ 
  9.  
  10.   function student(firstName, lastName) 
  11.   { 
  12.    this.fn = firstName; 
  13.    this.ln = lastName; 
  14.   } 
  15.  
  16.   function fun1() 
  17.   { 
  18.    var stu = new student("anjali", "Rani"); 
  19.  
  20.    alert(stu.fn); 
  21.    alert(stu.ln); 
  22.   } 
  23.  </script> 
  24.  
  25. </head> 
  26.  
  27. <body> 
  28.  
  29.  <button onclick="fun1();">Create Object</button> 
  30.  
  31. </body> 
  32.  
  33. </html>
Object constructor :- JavaScript Object constructor :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.