sudhakarinfotech
(current)
Run Code
Live Code Editor- Javascript Function In Prototype
Source Code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Javascript Prototype </title> </head> <body> <h1>Javascript Prototype </h1> <script type="text/javascript"> // constructor function function Person (name,age,job) { this.name = name; this.age = age; this.job = job; } // adding a method to the constructor function Person.prototype.userInfo = function() { document.write('hello my name is ${this.name} and my age is ${this.age} and working in the field of ${this.job}'); } // creating objects const person1 = new Person("ankita maurya",29,"non technical field."); const person2 = new Person("smith",30,"graphics designer"); person1.userInfo(); person2.userInfo(); </script> </body> </html>
Source Code Output :
Running