For unit-testing purposes, I need to overwrite the Number.prototype.toLocaleString()
so that it is always called with en-US
rather than default behavior. I understand overwriting a normal built-in function that receives a parameter, but toLocaleString()
does not take the Number to convert to string directly as parameter so I am confused here.
I tried the following:
Number.prototype.toLocaleStringTest = Number.prototype.toLocaleString
Number.prototype.toLocaleString = function() { this.toLocaleStringTest('en-US') }
But the result of this new toLocaleString()
function is always undefined
. New function is certainly called, by the way, I ensured this via adding a temporary console.log("I am called!")
in the implementation.
What am I doing wrong -- Or am I trying to achieve something that is not doable?
You were very close... Only a return
was missing... :-)
Number.prototype.toLocaleStringTest = Number.prototype.toLocaleString
Number.prototype.toLocaleString = function() {
return this.toLocaleStringTest('en-US')
}
n = 123456.789;
console.log(n.toLocaleString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With