lundi 12 janvier 2015

When is it sometimes useful to defining object types to mirror instance-bound methods as static utility functions on the constructor?


I am a beginner to JavaScript and currently going through the The Complete Reference 3rd Edition by Thomas A. Powell , Fritz Schneider .


I am on the way to learning Class Properties .


I quote an extract from the same book.

It is also sometimes useful when defining object types to mirror instance-bound methods as static utility functions on the constructor . For instance, String.prototype.trim() is the string instance method that operates on the instance it is called from. However, a static utility function such as String.trim() could be defined that takes as its only parameter the string instance it should operate on:



if(typeof String.trim == "undefined"){
String.trim = function(str){
alert("Here!");
return str.trim();
}
}

var test = " The International Jew ";
alert(test.trim()); // The International Jew
alert(String.trim(" The International Jew")); // The International Jew


I find myself really confused at what use does the above feature gives me and what will actually prompt me to do these kind of transformation from some instance method to static utility function ?

Kind guide me in understanding to this concept as I am a newbie into the programming world.





Aucun commentaire:

Enregistrer un commentaire