JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Object Prototypes

Prototypes are the mechanism by which JavaScript objects inherit features from one another.

In the previous chapter we learned how to use a constructor function:

Run code

We also learned that we cannot add a new property to an existing object constructor:

Run code

To add a new property to a constructor, you must add it to the constructor function:

Run code

Sometimes we want to add new properties and methods at later stage to a constructor function which will be shared across all the objects (instances). The answer is Object Prototype.

Using the prototype Property

The prototype property allows you to add properties and methods to constructor function.

In this example, the prototype property allows you to add a new property to the User object constructor:

Run code

In this example, the prototype property allows you to add a new method to the User object constructor:

Run code

Note: Only modify your own prototypes. Never modify the prototypes of standard (built-in) JavaScript objects.