JavaScript Prototype Example

The following is an example of a JavaScript program that demonstrates the use of prototypes:

In this example, a constructor function called "Person" is defined, which takes in two parameters, "firstName" and "lastName". These parameters are used to set the firstName and lastName properties of the Person object.

Next, a prototype method called "fullName" is defined on the Person object using the "prototype" property. This method concatenates the firstName and lastName properties and returns the full name of the Person.

A new Person object is created using the "new" keyword and the constructor function, with "John" and "Doe" passed as the firstName and lastName parameters, respectively.

Finally, the fullName method is called on the person1 object, which outputs "John Doe" to the console.

You can implement this program in any JavaScript development environment that supports the use of prototypes, such as a text editor with a browser or Node.js.


No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.