Usually you did not add anything to Object.prototype , because then it would be added to everything. This could be used, for example, with "".howSweetAmI() , and that would not make much sense.
Usually you have a specific type where you add methods to the prototype, for example:
function Fruit(color, shape, sweetness) { this.color = color; this.shape = shape; this.sweetness = sweetness; } Fruit.prototype.howSweetAmI = function() { return this.sweetness; }; var mango = new Fruit("yellow", "round", 8);
Adding a method to a prototype means that all instances use the same method. If you add a method as an instance property, only this instance has this method, and different instances can have different method implementations. For instance:
var mango = new Fruit("yellow", "round", 8); var banana = new Fruit("yellow", "bent", 70); mango.howSweetAmI = function() { return this.sweetness; }; banana.howSweetAmI = function() { return this.sweetness * 0.1; };
source share