how can you make an prototype on an Integer?
Integer.prototype.num = function(dec){
return number_format(this.toString(), dec);
}
prototype allows you to add new properties and methods to numbers. prototype is a property available with all JavaScript objects.
For the specification, integer only means that the numbers don't have a decimal fraction, and 32-bit means that they are within a certain range. For engines, 32-bit integer means that an actual integer (non-floating-point) representation can usually be introduced or maintained.
Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.
In JavaScript, every function and object has a property named prototype by default. For example, function Person () { this.name = 'John', this. age = 23 } const person = new Person(); // checking the prototype value console.
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):
Object.prototype
in this way. You'll break a huge amount of code.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
.
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() { }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With