spacer
home / programming / javascript / gr / column9 / 1 To page 1To page 2current page
[previous]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


How to Use JavaScript++

I would like to draw attention to the locale oriented functions. Since there is no concept of a ‘Locale’ object in JavaScript there is no way (in the script) to identify which locale to use when formatting the dates. The next best thing is to pick one locale and implement to that as this will be better than having the JavaScript code terminate due to an error.

New Number Methods

The Number object has gained a few methods recently too and like Date, they all relate to generating formatted strings…

 

if ( !Number.prototype.toFixed ) Number.prototype.toFixed = function(fractionDigits)

{

   var m = Math.pow(10,fractionDigits);

   return Math.round(this*m,0)/m;

}

if ( !Number.prototype.toExponential ) Number.prototype.toExponential = function(fractionDigits)

{

   var l = Math.floor(Math.log(this)/Math.LN10);

   var lm = Math.pow(10,l);

   var n = this / lm;

   var s = fractionDigits ? n.toFixed(fractionDigits) : n.toString();

   s += 'e' + ((l>0)?'+':'-') + l;

   return s;

}

if ( !Number.prototype.toPrecision ) Number.prototype.toPrecision = function(precision)

{

   var l = Math.floor(Math.log(this)/Math.LN10);

   var m = Math.pow(10,l + 1 - precision);

   return Math.round(this/m,0)*m;

}

New String Methods

The new String methods presented a few problems. In particular, comparison operators on strings (< and >) don’t follow the ordering of char codes, so the algorithm I've used to implement charCodeAt() iterates through all possible values until it finds one.

After studying the localeCompare() method, I found that it was not a straight forward comparison of strings. First the strings are compared without taking account of case, if different, the result of this comparison is returned. Otherwise the strings are compared again, this time sensitive to case. To give an example of the significance, “JavaScript” will be greater than “javascript” because while they are the same word, the capitalization of the first makes it greater. “Java” will be less than “javascript” because even though it has a capital “J”, the word is less.

 

String.prototype.caseValue = function()

{

   return (this.toLowerCase() == this) ? 0 : 1;

}

String.prototype.compareChars = function(a,b)

{

   if ( a.caseValue() < b.caseValue() ) return -1;

   if ( a.caseValue() > b.caseValue() ) return 1;

   if ( a < b ) return -1;

   if ( a > b ) return 1;

   return 0;

}

String.prototype.compare = function(that)

{

   var i = 0;

   while ( i < Math.min(this.length,that.length) )

   {

      var c = this.compareChars(this.charAt(i),that.charAt(i));

      if ( c != 0 ) return c;

      i++;

   }

   if ( this.length < that.length ) return -1;

   if ( this.length > that.length ) return 1;

   return 0;

}

if ( !String.prototype.localeCompare ) String.prototype.localeCompare = function(that)

{

   var sThis = this.toLocaleUpperCase();

   var sThat = that.toLocaleUpperCase();

   var r = sThis.compare(sThat);

   if ( r != 0 ) return r;

   return this.compare(that);

}

 

if ( !String.prototype.charCodeAt ) String.prototype.charCodeAt = function(index)

{

   var c = this.charAt(index);

   for ( var i = 0; i < 65536; i++ )

   {

      var s = String.fromCharCode(i);

      if ( s == c ) return i;

   }

   return 0;

}

 

if ( !String.prototype.toLocaleLowerCase )

   String.prototype.toLocaleLowerCase = String.prototype.toLowerCase;

if ( !String.prototype.toLocaleUpperCase )

   String.prototype.toLocaleUpperCase = String.prototype.toUpperCase;

New Object Methods

There are three new methods added to Object and of these, only the toLocaleString allows a simple script implementation…

 

if ( !Object.prototype.toLocaleString )

   Object.prototype.toLocaleString = Object.prototype.toString;

 

Unfortunately, isPrototypeOf() and hasOwnProperty() require too much access to the internals of the JavaScript object to to implement in script. My advice is to avoid using them.

 

Here's an example of the code in use.

Conclusion

In this article I’ve shown how many of the object methods only supported in the more recent browsers may be used in earlier browsers. In the early days of the browser wars, the browser implementers would stuff their products with whatever new feature came to mind in a desperate bit to win users. Each browser was rich with its own set of features, bugs and omissions. Fortunately, wisdom and the W3C prevailed and most modern browsers present a fairly level playing ground for JavaScript developers. With the help of the code shown above, this level ground may be reinserted into the older browsers too.

About the Author

Guyon Roche is a freelance web developer in London, Great Britain. He specializes in Windows platforms with an interest in bridging the gaps between different technologies, visit www.silver-daggers.co.uk for more details. He can be reached via e-mail at guyonroche@silver-daggers.co.uk


home / programming / javascript / gr / column8 / 1 To page 1To page 2current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27, 2003
Revised: September 29, 2004

URL: http://webreference.com/programming/javascript/gr/column9/1