I need a method in Math object of javascript which calculates the logarithm of any base. So basically what I did was this:
Math.log_b=function(b,x){return Math.log(x)/Math.log(b);}
What is the downside of extending built-in function like this?
To make my situation more clear, I am taking the user input and replacing it with appropriate Math object function names and passing it to eval for the calculation. If this is not clear, my dilemma is, in my case, I have to use eval (even if it is evil) and extending the Math object function best suits my case.
Is there possibility of some weird bugs or other when I extend the built-in function like this or is it the perfectly normal things to do?
I need a method in Math object of javascript which calculates the logarithm of any base. So basically what I did was this:
Math.log_b=function(b,x){return Math.log(x)/Math.log(b);}
What is the downside of extending built-in function like this?
To make my situation more clear, I am taking the user input and replacing it with appropriate Math object function names and passing it to eval for the calculation. If this is not clear, my dilemma is, in my case, I have to use eval (even if it is evil) and extending the Math object function best suits my case.
Is there possibility of some weird bugs or other when I extend the built-in function like this or is it the perfectly normal things to do?
Share Improve this question asked Jul 26, 2013 at 17:08 Jack_of_All_TradesJack_of_All_Trades 11.5k20 gold badges64 silver badges95 bronze badges 3- there is no Math() constructor so you cannot prototype it the math functions are only functions, not methods of an object. – Iron3eagle Commented Jul 26, 2013 at 17:11
- Yes, it cannot be prototyped. – Jack_of_All_Trades Commented Jul 26, 2013 at 17:12
- @Jack_of_All_Trades yes it can be prototyped. See my answer. – Naftali Commented Jul 26, 2013 at 17:12
2 Answers
Reset to default 7You should not modify what you don't own.
What happens if another plugin or 3rd party code you use adds their own version of
log_b
toMath
, which provides a pletely different signature?What happen if a future version of JavaScript defines it's own version of
log_b
onMath
?
Someone is going to cry, because for someone it wont do what they expect it to.
I'm not sure why extending Math
best suits your case.
function my_log_b(b,x){return Math.log(x)/Math.log(b);}
... still seems to suit your case. Even better, define your own namespace, and put it in there;
var ME = {};
ME.log_b = function (b,x){return Math.log(x)/Math.log(b);}
This is possible by defining the method:
Math.myMethod = (parameters) => {
// Code goes here
}
Example:
Math.addFive = (num) => {
return parseInt(num) + 5;
}
function calc() {
let g = document.getElementById("num").value;
document.getElementById("res").value = Math.addFive(g);
}
html {
background-color: teal;
}
<html>
<h1>Custom Math method "addFive()"</h1>
<label>Number:</label>
<input id="num" type="number">
<br><br>
<button onclick="calc()">Add five!</button>
<br><br>
<input type="number" readonly id="res">
</html>
Hope this was helpful!