How can I add the method to Math in javascript - Stack Overflow

I need a method in Math object of javascript which calculates the logarithm of any base. So basically w

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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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 to Math, which provides a pletely different signature?

  • What happen if a future version of JavaScript defines it's own version of log_b on Math?

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745528298a4631575.html

相关推荐

  • How can I add the method to Math in javascript - Stack Overflow

    I need a method in Math object of javascript which calculates the logarithm of any base. So basically w

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信