WARNING: This tutorial is being written! Do not hesitate to report any errors or suggestions.
Quick access:
Extension of the prototype associated with the Object builder
Extension of the prototype associated with the String builder
Extension of the prototype associated with the Array builder
Extension of the prototype associated with the Date builder
As we seen in the previous chapter, Javascript approach the notion of class through the concept of prototype. We have also seen that, because
the notion of class does not exist as such, it is possible to modify any object and to add to it new members at any moment.
As a Javascript prototype is an object itself, it is also possible to extend a prototype at any moment. Accordingly, it is
possible in Javascript, to enrich all the methods proposed for a type of object with new methods. The JWT library exploit this
possibility. This is the file corelib/services/web/javascript/Core.js that create the extensions of the existing prototypes.
Object.prototype.clone : allows to clone (duplicate an instance) by duplicating, recursively, all its characteristics.
var Rational = Object.extendClass( new Object(), {
initialize : function( numerator, denominator ) {
this.numerator = numerator || 0;
this.denominator = denominator || 1;
},
toString : function() {
return "[" + this.numerator + "/" + this.denominator + "]";
}
});
var original = new Rational( 3, 5 );
var clone = original.clone();
original.numerator *= 2;
original.denominator *= 2;
alert( original + " - " + clone ); // display : [6/10] - [3/5]Object.prototype.extendClass : as we seen in the previous chapter, this method makes it possible to implement the inheritance within the JWT library. The example above shows you an using example of this method. I also refer you to the previous chapter for more informations.
Object.prototype.extendPrototype : it allows to enrich an existing prototype. This method accepts a mandatory parameter: the members table to add to the considered prototype. This method also accepts an optional second parameter: it allows you to indicate, when a member of the same name already exists in the prototype, if we must replace it or not. By default the member is not replaced. This allows particularly, when there are differences between the browsers, to realign this method (functionally speaking) by adding a member only where the member does not exist. Here is a small example.
String.extendPrototype( {
left : function( pos ) {
if ( this.length <= pos ) return this;
return this.substring( 0, pos );
}
} );
alert( "theString".left(3) );String.prototype.left : can produce a new string of characters corresponding to the x firt characters of the string considered.
alert( "Langage Javascript".left( 7 ) ); // return "Langage"
String.prototype.right : can produce a new string of characters corresponding to the x last characters of the string considered.
alert( "Langage Javascript".right( 9 ) ); // return "Javascript"
String.prototype.trim : allows you to remove separators (white, tabs and returns to the line) present at the beginning and at the end of the string of characters considered.
alert( " \ttoto\n ".trim() ); // return "toto"
Array.prototype.contains : allows to test if a value table has an especially value or not. The result is
true or false.
var tb = new Array(); tb.push( "toto" ); tb.push( "titi" ); tb.push( "tata" ); alert( tb.contains( "toto" ) );
Date.prototype.getWeek : return the number of the week in the year.
alert( new Date().getWeek() );
|
|
|||||||