Integer prototype

后端 未结 3 1464
攒了一身酷
攒了一身酷 2020-12-13 21:48

how can you make an prototype on an Integer?

Integer.prototype.num = function(dec){
    return number_format(this.toString(), dec);
}
3条回答
  •  粉色の甜心
    2020-12-13 21:58

    Javascript has no integral types (Integer). Only floating point numbers (try typeof 3. Returns Number).

    So you could use something like:

    Number.prototype.myfunc = function() { }
    

    Addendum

    As Felix Kling mentioned in the comment, extending Javascript built in objects using the .prototype property is usually not a good idea. The new extension applies automatically to all objects of this type - including those already instantiated and the nones that will be created by code other than yours. Thus, it might interfere with what some other code is expecting of the Number object.

提交回复
热议问题