How to override javascript function

I am trying to override the parseFloat built-in function in js. How should I do it?

+87
javascript
Mar 23 '11 at 17:45
source share
3 answers
var origParseFloat = parseFloat; parseFloat = function(str) { alert("And I'm in your floats!"); return origParseFloat(str); } 
+206
Mar 23 '11 at 17:50
source share

You can override any built-in function by simply re-declaring it.

 parseFloat = function(a){ alert(a) }; 

Now parseFloat(3) will warn 3.

+39
Mar 23 '11 at 17:50
source share

You can do it as follows:

 alert(parseFloat("1.1531531414")); // alerts the float parseFloat = function(input) { return 1; }; alert(parseFloat("1.1531531414")); // alerts '1' 

Check out the working example here: http://jsfiddle.net/LtjzW/1/

+8
Mar 23 '11 at 17:49
source share



All Articles