Overriding a JavaScript global function - parseInt
Written by Ian Elliot   
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


JavaScript Jems - Objects Are Anonymous Singletons

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, every object can be regard [ ... ]



JavaScript Canvas - Typed Arrays

Working with lower level data is very much part of graphics. This extract from Ian Elliot's book on JavaScript Graphics looks at how to use typed arrays to access graphic data.


Other Articles

<ASIN:0137054890>

<ASIN:0596517742>

<ASIN:1590597273>



Last Updated ( Tuesday, 21 December 2010 )