Extending Number, Revisited
4 months ago, I wrote an article extending the native JavaScript Number Object with a format method:
Numbers and Number Format Function
I have revisited that method and also added several other methods to Number.prototype and Number that I find useful. Check out the new Number.js here and the unit test here if you want to double check that it all works well on your favorite browser. Mostly, there is the format method and shortcuts to Math functions.
You may have noticed on Date.js and Number.js that I have been adding an ‘is’ method to the actual native Object for type detection (not the ‘prototype’). This is a compromise that I made with myself, because I do not want a bunch of ‘isType’ methods in the global namespace, but at the same time I find it tedious to always type “isType(object, ‘type’)”. So, instead of putting a bunch of ‘isType’ functions in the global namespace, I am attaching ‘is’ methods to the native Object for a given type. So, with Number, instead of using “isType(object, ‘number’)”, I can just write “Number.is(object)”. This works for all Object types, including ‘object’, because the method is not attached to the ‘prototype’ Object, thereby not breaking for…in loops. And it is an especially useful shortcut, if your type detection is attached to a nested namespace, such as the YAHOO type detection attached to the “YAHOO.lang” Object.

A while ago I found a neat little method for commafying numbers on Steven Levithan’s website “Flagrant Badassery.” I haven’t tested it for speed, but when my own implementation took 15-20 lines of code, and this one takes about 5 lines of code, it instantly grabbed my attention.
http://blog.stevenlevithan.com/archives/commafy-numbers
I don’t generally like taking code from other people, but I simply couldn’t think of a better (more concise) solution myself. I always just make sure I comment where/who the code comes from.
Comment by MillsJROSS — July 16, 2008 @ 5:27 am