Object Prototype

The prototype property allows you to add new properties to an existing prototype.

Code Structure
function person(firstName, lastName, age) {
 this.firstName = firstName;
 this.lastName = lastName;
 this.age = age;
}
person.prototype.nationality = "English";
var myfriend = new person("Bill", "Gaates", 25);
alert("His nationality name is " + myfriend.nationality);

The prototype property allows you to add new method to an existing prototype.

function person(firstName, lastName, age) {
 this.firstName = firstName;
 this.lastName = lastName;
 this.age = age;
}
person.prototype.name = function() {
 return this.firstName + " " + this.lastName
};
var myfriend = new person("Bill", "Gates", 25);
alert("His full name is " + myfriend.name());

Share this

Related Posts