- 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:-
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script>
- /*When you create the object the constructor call automatically.*/
- function student(firstName, lastName)
- {
- this.fn = firstName;
- this.ln = lastName;
- }
- function fun1()
- {
- var stu = new student("anjali", "Rani");
- alert(stu.fn);
- alert(stu.ln);
- }
- </script>
- </head>
- <body>
- <button onclick="fun1();">Create Object</button>
- </body>
- </html>
Object constructor :- JavaScript
Reviewed by Network security
on
November 13, 2019
Rating:
No comments: