User Tools

Site Tools


javascript_object_prototypes

Javascript and React

Javascript Object Prototypes

Prototype Inheritance

All JavaScript objects inherit properties and methods from a prototype.

Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.

The Object.prototype is on the top of the prototype inheritance chain:

Date objects, Array objects, and Person objects inherit from Object.prototype.

Adding properties and functions using a prototype

You can modify a prototype to affect all its “children”

*Note: Only modify your own prototypes. Never modify the prototypes of standard JavaScript objects.

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

Person.prototype.nationality = "English";

Person.prototype.name = function() {
    return this.firstName + " " + this.lastName;
};

Prototype Properties

from https://www.w3schools.com/js/js_object_properties.asp

JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.

javascript_object_prototypes.txt · Last modified: 2018/09/07 14:38 by admin