spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column19 / 1

To page 1current pageTo page 3
[previous][next]

A Workaround

Having established that inheritance isn't going to work with JavaScript intrinsic classes, we need to explore the alternatives.

The next best thing to inheriting from an intrisic class is inheriting from a class that behaves in the same manner. If a class exists that implements all the properties and methods of another class and in the same way, then this class could be substituted for the other.

Consider the JavaScript String class. It supports a length property and a number of methods for processing the string data.

var s = "Hello World";

var n = s.length;        // n becomes 11

var s2 = s.substr(6);    // s2 becomes "World"

alert(s);                // pops up a message box with "Hello World"

var s3 = 'Msg: ' + s;    // s3 becomes "Msg: Hello World"

To create a wrapper class that can simulate a String, all that's needed is to support the property and the methods:

// String wrapper class

function StringWrapper(value)

{

   // store the value

   this.String = value;

 

   // support the length property

   this.length = this.String.length;

}

StringWrapper.prototype.substr = function(start, length)

{

   return this.String.substr(start,length);

}

// etc...

New StringWrapper instances can be created with the new operator:

var s = new StringWrapper("Hello World");

The StringWrapper has almost everything it needs to simulate the real String instance in the code snippet above. It's constructed with a string value which is stored under a property called "String". The length property is supported by assigning its value in advance during the constructor. Since the String contents don't change during the lifetime of the StringWrapper, the string length will remain constant as well. The substr function simply calls into the String version with the supplied arguments.

There is still a problem with the StringWrapper class as it stands. If a StringWrapper instance is passed to the alert() function, the resulting window will not display "Hello World" as expected but something like "[object Object]". The reason for this is that when alert() is called, it will first call toString() on the argument to translate it into string form so that it can be displayed. The StringWrapper class must implement this function...

StringWrapper.prototype.toString = function()

{

   return this.String;

}

The string expression "Msg: " + s needs some further explanation. When JavaScript encounters an expression, the first thing it will attempt to do is call a function called valueOf() on each operand in order to convert them into intrinsic values. If that fails or if the resulting value is too complex (like an Array or Date object), the JavaScript implementation may attempt other routes. In this example, the intrinsic result type of the expression will be a string because at least one of the operands is a string. In this case the JavaScript engine may elect to convert the values directly to a string using the toString() function. In any case, the StringWrapper class should implement valueOf()

StringWrapper.prototype.valueOf = function()

{

   return this.String;

}

 

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column19 / 1

To page 1current pageTo page 3
[previous][next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: April 28, 2006

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