Extending Number.prototype in javascript and the Math object?

后端 未结 8 1746
孤城傲影
孤城傲影 2021-02-05 03:11

I\'ve always wondered why Javascript has the global Math object instead of giving numbers their own methods. Is there a good reason for it?

Also are there any drawbacks

8条回答
  •  隐瞒了意图╮
    2021-02-05 03:23

    You can do this

    Number.prototype.round = function() {
        return Math.round(this.valueOf());
    };
    

    https://www.w3schools.com/jsref/jsref_prototype_num.asp

提交回复
热议问题