146

I'm trying to override a built in parseFloat function in JavaScript.

How would I go about doing that?

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Riz
  • 6,486
  • 19
  • 66
  • 106

5 Answers5

267
var origParseFloat = parseFloat;
parseFloat = function(str) {
     alert("And I'm in your floats!");
     return origParseFloat(str);
}
David Waters
  • 11,979
  • 7
  • 41
  • 76
  • Is it also possible to call the native version inside a custom `parseFloat`? I tried this but it errors 'Maximum call stack size exceeded': http://jsfiddle.net/huZG2/. Thanks. – pimvdb Mar 23 '11 at 17:52
  • 2
    @David Waters: I'm afraid your current code alerts forever in Chrome. – pimvdb Mar 23 '11 at 17:55
  • 13
    @David Keep in mind that function are hoisted, which mean that `origParseFloat` points to the function you declare right after. [This](http://jsfiddle.net/huZG2/2/) would work. – HoLyVieR Mar 23 '11 at 17:58
  • @HoLyVieR: Thanks, I see it's possible to both override and use the native `parseFloat`: http://jsfiddle.net/huZG2/3/. – pimvdb Mar 23 '11 at 18:02
  • 1
    @HoLyVieR thanks Fixed by assigning function to name answer updated see http://jsfiddle.net/huZG2/6/ – David Waters Mar 23 '11 at 18:30
  • If you want to keep the original function anonymous and be able to use it only in your overriding, you can see another answer: http://stackoverflow.com/questions/296667/overriding-a-javascript-function-while-referencing-the-original – Chop Nov 12 '14 at 08:53
  • 1
    This is not a function override. Overrides do not alter the original function. This is an overwrite. – tim-montague Jul 03 '21 at 18:05
48

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

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

Now parseFloat(3) will alert 3.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
37

You could override it or preferably extend it's implementation like this

parseFloat = (function(_super) {
    return function() {
        // Extend it to log the value for example that is passed
        console.log(arguments[0]);
        // Or override it by always subtracting 1 for example
        arguments[0] = arguments[0] - 1;
        return _super.apply(this, arguments);
    };         

})(parseFloat);

And call it as you would normally call it:

var result = parseFloat(1.345); // It should log the value 1.345 but get the value 0.345
Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
12

You can do it like this:

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

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

Kyle Wild
  • 8,845
  • 2
  • 36
  • 36
8

An override is a concept that comes from object-oriented programming, where inheritance is used to extend class methods

Javascript has classes (and prototype inheritance), but parseFloat is simply a function and not a class (or a prototype). So you would either need to make parseFloat a class method, or override the related Number.parseFloat method.

Let's explore this further. An override method, does not mutate (extend or overwrite) the original parent method. As illustrated in the following example:

class A {
  // parent method
  print() {
    console.log("class A");
  }
}

class B extends A {
  // override method
  print() {
    console.log("class B");
  }
  parentPrint() {
    super.print();
  }
}

const b = new B();
b.print(); // prints "class B" from override method
b.parentPrint(); // prints "class A" from parent method

To apply this to Number.parseFloat, we could do:

class B extends Number {
  // overrides `parseFloat` from Number class
  parseFloat() {
    super.parseFloat();
  }
}

const b = new B();
b.parseFloat();

However, modern practice is to favor composition over object-oriented inheritance.

Inheritance is regarded as confusing, fragile, and less flexible when compared to composition. React (Facebook) and Go (Google) programming languages for example, both encourage composition:

Therefore my basic recommendation is to use composition:

const parseFloatOverride = function () {
  return parseFloat();
};

And my extended recommendation is use composition with dependency injection, to create loosely coupled dependencies which are more suitable for unit testing.

// Inject the `Number` dependency
const parseFloatOverride = function (Number number) {
  return number.parseFloat();
};

Warning: do not overwrite the Javascript core library

Also almost every answer on this thread overwrites the parseFloat function. That's bad practice, because (a) developers expect parseFloat to work as documented, and (b) that includes developers who wrote any third-party packages you might use, that would now be corrupted. So do not overwrite the Javascript core library, and instead use composition or inheritance.

const parseFloat = function () {}; // Bad practice
const Number.prototype.parseFloat = function () {}; // Also bad practice
tim-montague
  • 16,217
  • 5
  • 62
  • 51