Overriding a JavaScript global function - parseInt
Monday, 20 December 2010
Article Index
Overriding a JavaScript global function - parseInt
The new parseInt
A refinement

Banner

Second attempt

The function we have so far is good but you have to remember to call updateparseInt - not difficult but we can do better. Ideally what we would like is to directly define  the parseInt function so that it modifies itself the first time it is called, i.e. no need to remember to call another function.

You might think that all you need to do is redefine the parseInt function as in

parseInt=function{
 var tempBase= parseInt;
   ...

but this doesn't work because the function object is assigned to the parseInt variable before the function is executed at some later data. That is tempBase ends up referencing the new parseInt function not the old one - if you think about when the function is executed it should be obvious.

What you have to do is use a function that is excuted imediatly to set parseInt to a function that has access to the old parseInt function. That is:

parseInt=function{
var tempBase= parseInt;
     ...
  return new function
}();

Notice that the outer function ends with () which causes it to execute imediately and it returns the function that parsInt is set to. In this case the local variable tempBase is set to the old value of parseInt.

Once again we can use closure to all the new function to access the old paresInt function. In this case the whole thing is very nearly the same as the first function presented. 

First we check that the change to the pareInt function is necessary:

 parseInt=function(){
  if(parseInt("010")===10)return ;

if it isn't we simply return and leave the function as it is.

If it is we store the old reference in tempBase and return the new function:

  var tempbase=parseInt;
  return (function(n,r){

The rest of the new function is exactly what it was before:

  if (r === undefined) {
   n = n.replace(/^s*/, "");
   if (n.substr(0, 2) === "0x") {
    r = 16;
   }
   else {
    r = 10;
   }
  }
  return tempbase(n,r);
 });

Finally we have to close and execute the outer function:

}();

Yes I agree it seems like magic, only this time doubly so but it isnt'. It is perfectly good JavaScript making use of its functional and dynamic aspects.

Notice there is an overhead in on every call to the new parseInt in that it the first if statement evaluates parseInt just to check it is working (this was the case with the first version as well).

The full listing of the function is:

parseInt=function(){
 if(parseInt("010")===10)return ;    
 var tempbase=parseInt;
 return (function(n,r){
  if (r === undefined) {
  n = n.replace(/^s*/, "");
  if (n.substr(0, 2) === "0x") {
   r = 16;
  }
  else {
  r = 10;
  }
}
return tempbase(n,r);
});

And you can test it in the same way:

alert(parseInt);
alert(parseInt("0xFF"));
alert(parseInt("111",2));
alert(parseInt("023"));
alert(parseInt("09"));

You can use the same technique to redefine any function while retaining the ability to call the old version of the function to get some of the work done. However doing this in general isn't a good idea unless you are attempting to bring JavaScript into line which what it will become in future versions.

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.

 

Banner


Just JavaScript - The Prototype Mechanism

The prototype is about the most mysterious part of JavaScript. Once you have mastered the call context and the constructor, it is the prototype that you have to turn to. How does it work? How do you u [ ... ]



Getting Started With jQuery - Advanced Filters

When you first encounter filters they seem easy enough - just extract the results you want from the results you have. The trouble is that filters are fun and jQuery pushes the idea beyond the obvious. [ ... ]


Other Articles

<ASIN:0137054890>

<ASIN:0596517742>

<ASIN:1590597273>



Last Updated ( Tuesday, 21 December 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.