A quick exploration of Javascript's prototyping mechanism.
I Javascript is sufficient object aware that object oriented design can be done with the language. The semantics for OO Javascript are alien to most of the current popular OO languages such as Java, C# and Python.
The main difference is the lack of a class keyword in Javascript so a class declaration is done with a function instead and the methods of the object are not bounded by the brackets {} or whitespace under the class keyword. One of the good things of the C-like languages and python's white space is that it makes for an aesthetic rigor so that encapsulation is held within a physical space in the source code, not just a compiler recognised one.
Rather than having a rats nest of nested literal functions inside an object declared with a function in Javascript the prototype property enables semantic and aesthetic appearance of encapsulation.
The prototype allows attributes and methods to be attached to an object. For instance:
function Jot(username, description_html) {
this._username = username;
this._description_html = description_html;
}
Jot.prototype.getDescription = function() {
return this._description_html.replace('<p>','\n');
} Which exposes the object's members username and description_html and the method which returns the description without paragraph tags.
II Prototyping can also be used to add functionality to existing or core objects whose internal details you have no control over. This can be done without the need for subclassing the object and having to use inheritance to achieve that behavior.
Adding prototype functionality to the Date() object is a good example. A common cause of confusion between American and Australian dates is that the Month and Date are in opposite positions when dates are written in local shorthand.
For instance:
Date.prototype.getAustralianDate = function() {
return (this.getDate() + '/'
+ this.getMonth() + '/'
+ this.getFullYear());
}
Date.prototype.getAmericanDate = function() {
return (this.getMonth() + '/'
+ this.getDate() + '/'
+ this.getFullYear());
} And to test it:
d = new Date(); alert(d.getAustralianDate() + "\n" + d.getAmericanDate());The alert shows the Australian style of date on the first line and the American style on the second line. III Does a prototype declaration over-ride a variable of the same name being set in a javascript constructor?
function Rock() {
this.face = "mossy";
}
rock = new Rock();
alert(rock.face);
Rock.prototype.face = "dry";
alert(rock.face);
rock2 = new Rock();
alert(rock2.face);
The output is "mossy", "mossy" and "mossy". So the answer is no.
What about an alert that includes both this.face and prototype.face:
rock = new Rock(); Rock.prototype.face = "dry"; alert(rock.face + " " + Rock.prototype.face);This returns "mossy dry". However if there is no public variable declared in the Javascript's object constructor:
function Rock() {
this.weight = 10; // kgs
}
rock = new Rock();
Rock.prototype.face = "dry";
alert(rock.face); The answer is "dry".




