Using prototype with String object– JavaScriptActual Blog post link: http://jbkflex.wordpress.com/2013/04/16/using-prototy...
String.prototype.removeEmptySpaces = function(){return this.replace(/s+/g,);}}As you can see above, first we check if the ...
First we create a String Object instance – testStr and pass it a value. Then we call ourcustom method and store the return...
Upcoming SlideShare
Loading in...5
×

Using prototype with String object – JavaScript

133

Published on

Every object in JavaScript has the prototype property which enables you to enhance the functionality of the current Object definition (the Class definition) either through adding properties or through methods. This stands true for both custom Objects as well as JavaScript defined Objects like Strings, Arrays.
Using prototypes to add functionality also has one more advantage and is performance optimized. Whatever methods or properties are defined using prototypes, they belong at the class level which means that all the instance of the Object (or rather the Class, in JavaScript there is no concept of Class, we say it Objects) will share the same copy of the method or property, rather than having their own individual copies.
For the full post download this file or visit the actual blog post link: http://jbkflex.wordpress.com/2013/04/16/using-prototype-with-string-object-javascript/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
133
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Using prototype with String object – JavaScript"

  1. 1. Using prototype with String object– JavaScriptActual Blog post link: http://jbkflex.wordpress.com/2013/04/16/using-prototype-with-string-object-javascript/Every object in JavaScript has the prototype property which enables you to enhancethe functionality of the current Object definition (the Class definition) either throughadding properties or through methods. This stands true for both custom Objects aswell as JavaScript defined Objects like Strings, Arrays.Using prototypes to add functionality also has one more advantage and is performanceoptimized. Whatever methods or properties are defined using prototypes, they belongat the class level which means that all the instance of the Object (or rather the Class,in JavaScript there is no concept of Class, we say it Objects) will share the same copyof the method or property, rather than having their own individual copies.In this post I am going to enhance the String Object which is already defined inJavaScript and has a lot of useful methods already available such as the split(),indexOf(), replace() etc and properties like length etc.Now let’s add our new method to the String Object just to enhance its functionality.Let’s add a method that will remove the empty spaces between the words of a stringvalue. For eg.input value = The weather is very niceoutput value = TheweatherisveryniceDefining our custom methodAs you guessed it right we will be using the prototype property. Let’s try this thingout,if(!String.prototype.removeEmptySpaces){
  2. 2. String.prototype.removeEmptySpaces = function(){return this.replace(/s+/g,);}}As you can see above, first we check if the removeEmptySpaces() method is alreadydefined. If not (which is true in this case since we are adding out custom method. Butthe check is good to have if you do not want to overwrite a predefined method) thenwe define it inside the if block. So using the prototype property we add the custommethod and from now on it is available to all the instances of the String Object. Insidethe method definition we have a String.replace() method implemented. This willremove every occurrence of space between the words and will join them. I am using aregular expression inside the replace() method to match the spaces between the words.Once the match is found, it will be removed. You can also see the use of thiskeyword.This will represent the current instance that is calling our custom method- removeEmptySpaces().You can learn more about the replace() method from w3schools.comPutting it to testNow let’s use our new method and see if it is actually doing the stuff that we wanted.var testStr = new String("The weather is very nice"); //create a newinstancevar newStr = testStr.removeEmptySpaces(); //call our custom methodalert("New string after removing empty spaces is : " + newStr);
  3. 3. First we create a String Object instance – testStr and pass it a value. Then we call ourcustom method and store the returned value in newStr. Now if you alert the newvalue you can see that the empty spaces between the words have been removed. Herewe have seen using our customized method with a String instance. What about Stringliterals or constants? Well, we can use for them as well. Here is an example,var strConstant = "I am a String constant"; //defining our string literalvar newStrConstant = strConstant.removeEmptySpaces();alert(newStrConstant); //alerts "IamaStringconstant"So, we have seen how using prototypes we can add new functionality to our Objects.This was a simple example that I presented. You can customize any Objects as peryour need.

×