开发者

Integer prototype

how can you make an prototype on an Integer?

开发者_StackOverflow中文版Integer.prototype.num = function(dec){
    return number_format(this.toString(), dec);
}


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 prototypes. 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:

  • Never, ever, ever extend Object.prototype in the way you've shown (by assignment). You'll break a huge amount of code.
  • Be very, very wary of extending Array.prototype in the way you've shown, 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 the way you've shown, you'll break their loops. (I would argue their loops were already broken, but leave that to the side...)

I keep saying "in the way shown" because you can add non-enumerable properties to objects via 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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜