Recently I started learning Javascript as I am comming from Java world. I got this book called JavaScript The Definitive guide.. I am now a little confused about Prototypes and Inheritance. I want to know what is difference between setting a function to object property and functions prototype. From the book example :
function Rectangle(w, h) {
this.width = w;
this.height = h;
this.area = function( ) { return this.width * this.height; }
}
With this new version of the constructor, you can write code like this:
// How big is a sheet of U.S. Letter paper in square inches?
var r = new Rectangle(8.5, 11);
var a = r.area( );
This solution works better but is still not optimal (why) . Every rectangle created will have three properties ( Yes, and so what ? ). The width and height properties may be different for each rectangle, but the area of every single Rectangle object always refers to the same function (someone might change it, of course, but you usually intend the methods of an object to be constant). It is inefficient to use regular properties for methods that are intended to be shared by all objects of the same class (WHY ??? What is the problem ? ) (that is, all objects created with the same constructor).