"Prototype" is what plays a role in objects.
In Javascript, everything is an object. Each object has a form and, therefore, inherits a prototype this type.
For example, take a simple array: var a = [] . You can perform operations with it, for example a.push(10) . Where does this push method come from? From the prototype of the Array object, which a is.
You can add your own methods to Array objects by simply specifying them in the prototype object. For example:
Array.prototype.sortNum = function() {this.sort(function(a, b) {return a - b});};
This way you can do something like a.sortNum() with all arrays, even those that were created before you defined the sortNum method.
(Note: for compatibility reasons, it is usually not recommended to extend the prototype of your own objects, such as Array s. But this specific example is usually a nice addition, as well as the normalization of methods such as map and forEach for older browsers.)
(Just never expands Object.prototype ! If you don't want to mess up the for...in statements, the in statement and similar cases.)
If you want to define your own classes, as the name MyConstructor , you will need to define its prototype in order to define methods for all instances of this class:
function MyConstructor(name) {this.name = name}; MyConstructor.prototype = { print: function() {return this.name;} }; var mc = new MyConstructor("foo"); alert(mc.print());
You can define more than just functions in prototype s:
MyConstructor.prototype.age = 30; alert(mc.age); // alerts 30
Beware when you do this to determine the default values ββof an object, since changing it can lead to change in all instances of this class.
But this is convenient when using Object.defineProperty :
Object.defineProperty(MyConstructor.prototype, "wholeString", { get: function() {return this.name + "=" + this.age;}, set: function(v) {this.name = v.substring(3);} }); alert(mc.wholeString);
(Unfortunately, IE <9 only allows this for DOM objects ...)
When you define MyConstructor.age = 30 instead, what you are actually doing is defining a member of the MyConstructor function, so mc.age will be undefined. Each instance of MyConstructor inherits the methods and members defined in MyConstructor.prototype , and not those functions of MyConstructor .
In fact, much more can be said. Objects can be a subclass of another class, thus inheriting the prototype superclass. For example, document.body is an instance of HTMLBodyElement , which is a subclass of HTMLElement , which is a subclass of Element , etc., until you get Object as the top superclass. Thus, document.body inherits all the methods defined in the prototype HTMLBodyElement , HTMLElement , Element and Object . This is called the prototype chain.
Doing the same with custom objects is a bit complicated:
function Class() {}; Class.prototype.foo = function() {alert("foo");}; function Subclass() {}; Subclass.prototype = new Class(); Subclass.prototype.bar = function() {alert("bar");}; var a = new Class(), b = new Subclass(); a.foo();