Click here to Skip to main content
15,896,453 members
Articles / Web Development / HTML

Extending JavaScript Intrinsic Objects with Prototypes

Rate me:
Please Sign up or sign in to vote.
4.90/5 (13 votes)
17 Oct 2006CPOL18 min read 74K   297   43  
Distillate your own JavaScript flavor with prototypes.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// file: number.js
// desc: Number prototypes
// auth: H�ctor J. Rivas torjo2k@hotmail.com
// updt: Mon Feb 28 17:22:48 UTC-0400 2005
//		 Wed Apr 20 16:33:11 UTC-0400 2005
// 		 Tue Oct 17 16:25:05 CDT 2006 The Code Project version
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// string method wrappers
Number.prototype.zp = function(n) { return this.toString().zp(n); }
Number.prototype.zt = function(n) { return this.toString().zt(n); }
Number.prototype.substr = function(n) { return this.toString().substr(n); }
Number.prototype.reverse = function() { return this.toString().reverse(); }

// number sign 'bit' (boolean)
Number.prototype.sign = function() { return this < 0; }

// decimal digits truncation
Number.prototype.truncate   = function(n) { return Math.round(this * Math.pow(10, n)) / Math.pow(10, n); }

// fractional part of a number
Number.prototype.fractional = function()  { return parseFloat(this) - parseInt(this); }

// integer thousand separators
Number.prototype.group = function()
{
	var s = parseInt(this).reverse(), r = '';

	for (var i = 0; i < s.length; i++)
		r += (i > 0 && i % 3 == 0 ? ',' : '') + s.charAt(i);
	
	return r.reverse();
}

// format a number with n decimal digits, thousands separator and sign
Number.prototype.format = function(n)
{
	// remember the input sign and cancel it
	var a = Math.abs(this);

	// truncate and zero-trail the fractional part
	var f = a.fractional().truncate(n).substr(2).zt(n);

	// sign + grouped integer part + dot + fractional part
	return (' -'.substr(this.sign(), 1) + a.group() + '.' + f).trim();
}

// math cosecant, secant and cotangent
Math.csc = function(x) { return 1 / Math.sin(x); }
Math.sec = function(x) { return 1 / Math.cos(x); }
Math.cot = function(x) { return 1 / Math.tan(x); }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Texas Capital Bank
United States United States
Professional software engineer with 30+ years of experience delivering systems across diverse industries, looking for the next opportunity to deliver cutting edge end-to-end technology solutions.

Avid reader, disciplined writer and enthusiastic tinkerer with a background in electronics, looking inside and thinking outside the box, genuinely passionate about robust, extensible, reusable and performant code.

Framework developer leading, coaching and learning about best practices, code quality, DevOps and software and data lifecycle management with an agile mindset to create the most elegant and sustainable solutions.

Comments and Discussions