Integer prototype

后端 未结 3 1454
攒了一身酷
攒了一身酷 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.

    0 讨论(0)
  • 2020-12-13 22:04

    Integer is not a native javascript Object. Maybe you mean adding a formatting method to the prototype of Number? For example:

    var formatNum = function(format){
       return format === 'dec2'
               ? this.toPrecision(String(this).length+2)
               : format === 'div10'
               ? Number(this/10).toPrecision(2)
               : this;
    }
    Number.prototype.format = formatNum;
    var num = 22;
    num.format('dec2'); //=> 22.00
    num.format('div10'); //=> 2.2
    
    0 讨论(0)
  • 2020-12-13 22:06

    There's no Integer in JavaScript, just Number. You can extend Number in the way you've shown.

    There are two camps with regard to extending the built-in types in this way. One camp says it's evil and you must not do it; use a separate function you pass the object into. The other camp says this is exactly why we have prototypes, so that we can extend the functionality of things as we see fit.

    If you're in the second camp, there are pitfalls to avoid (but see "The world is changing..." below):

    • Never, ever, ever extend Object.prototype in this way. You'll break a huge amount of code.
    • Be very, very wary of extending Array.prototype, you'll break a fair bit of code.

    The reason in both cases is that when you extend a prototype in that way, you create an enumerable property on the prototype. That means it shows up in for..in loops. Custom and practice in the JavaScript world is that there must be no enumerable properties on a blank object ({}). Arrays may be more acceptable, but beware people who don't really understand what for..in does who think it will loop through the array indexes; if you extend Array.prototype in this way, you'll break their loops. (I would argue their loops were already broken, but leave that to the side...)

    The world is changing, though, because ECMAScript5 gave us a way to add non-enumerable properties to objects, and all vaguely up-to-date browsers implement it. It's called Object.defineProperty:

    Object.defineProperty(Number.prototype, "num", {
        enumerable: false,
        value: function() { ... }
    });
    

    Note that enumerable: false (false is the default for enumerable; I'm being explicit here for emphasis). That means it doesn't show up on for..in loops, and so softens that argument against extending prototypes of native objects.

    However, the possibility of naming conflicts (with other code also extending the prototype, or features added in future versions of JavaScript) remains, even with Object.defineProperty.

    0 讨论(0)
提交回复
热议问题