Learn JavaScript Part 21: Extension Method

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5. </head>  
  6. <body>  
  7.     <script id="javascript">  
  8.         String.prototype.getLength = function () {  
  9.             return this.length;  
  10.         }  
  11.   
  12.         var str = "Akshay Patel";  
  13.   
  14.         alert(str.getLength());  
  15.     </script>  
  16. </body>  
  17. </html> 

Output

12