WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Object Prototypes


Every JavaScript object has a prototype. The prototype is also an object.

All JavaScript objects inherit their properties and methods from their prototype.


JavaScript Prototypes

All JavaScript objects acquire the properties and strategies from their prototype.

Objects made utilizing an article strict, or with new Object(), acquire from a model called Object.prototype.

Objects made with new Date() acquire the Date.prototype.

The Object.prototype is on the highest point of the model chain.

All JavaScript objects (Date, Array, RegExp, Function, ....) acquire from the Object.prototype.


Creating a Prototype

The standard approach to make an item model is to utilize an article constructor function:

Example

work person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}

With a constructor work, you can utilize the new catchphrase to make new articles from the equivalent prototype:

Example

var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
Try it Yourself »


Adding Properties and Methods to Objects

Sometimes you need to include new properties (or techniques) to a current object.

Sometimes you need to include new properties (or techniques) to every current object of a given type.

Sometimes you need to include new properties (or techniques) to an article prototype.


Adding a Property to an Object

Adding another property to a current article is simple:

Example

myFather.nationality = "English";
Try it Yourself »

The property will be added to myFather. Not to myMother. Not to some other individual objects.


Adding a Method to an Object

Adding another strategy to a current article is additionally simple:

Example

myFather.name = work () {
    return this.firstName + " " + this.lastName;
};
Try it Yourself »

The strategy will be added to myFather. Not to myMother.


Adding Properties to a Prototype

You can't add another property to a model indistinguishable path from you include another property to a current article, on the grounds that the model isn't a current object.

Example

person.nationality = "English";
Try it Yourself »

To add another property to a constructor, you should add it to the constructor function:

Example

work person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
    this.nationality = "English"
}
Try it Yourself »


Adding Methods to a Prototype

Your constructor capacity can likewise characterize methods:

Example

work person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
    this.name = work() {return this.firstName + " " + this.lastName;};
}
Try it Yourself »

Using the prototype Property

The JavaScript model property enables you to add new properties to an existing prototype:

Example

work person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}
person.prototype.nationality = "English";
Try it Yourself »

The JavaScript model property likewise enables you to add new techniques to an existing prototype:

Example

work person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}
person.prototype.name = work() {
    return this.firstName + " " + this.lastName;
};
Try it Yourself »