Via JScript team in Microsoft:
JScript is object oriented. All object oriented languages allow us to define a Class and then create individual objects that are instances of that Class. So does JScript allow us to define Classes and create instances?
JScript does not support true Classes the way most traditional languages do. However it is possible to simulate Classes in JScript using constructor functions and prototype objects.
function Rectangle (ht, wt)
{
this.height = ht;
this.width=wt;
this.area = function() { return this.height * this.width; }
}
Here every Rectangle object created will have three properties - height, width and area. height and width for each Rectangle object might differ but area property would always refer to the same function object. What this means is area property is going to be common for any number of Rectangle objects. Then why can't we have area as a shared property? We can and this is where prototype objects come into picture! Let's define area as a property to the prototype object of Rectangle:
Rectangle.prototype.area = function() {return this.height * this.width;}
This has couple of advantages:
- Since all common methods (ones that can be shared by all objects) are defined as properties to the prototype object and are directly inherited by the objects, there is good amount of decrease in memory usage.
- In case a property is added to the prototype object after an object was created, then too the object would inherit the newly added property.