spacer

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


Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Logo

JavaScript Programming: dltypeof() v1.0,
An Introduction

Produced by Peter Belesis


Prerequisite

To fully appreciate the functionality of dltypeof(), you should be familiar with the JS operator it attempts to replace: typeof().

The typeof() operator is defined in:

  1. The ECMAScript Documentation, pages 46-47.

  2. The Netscape JavaScript Documentation.

  3. The Microsoft JScript Documentation.

The JS typeof() problem

Anyone who has written a JS application of any complexity will have come across the frustrations associated with the limitations of the typeof() operator.

The typeof() operator performs as defined, but we are left wishing that it could provide us with more meaningful information, or that a similar, more powerful, operator existed to help identify object types to our applications.

For example, if we have a function that expects either a string, an array or null as its single argument, how do we identify the argument's type in our function? Our function could look like this:

function myFunction( vArgument )
{
    var sTypeOf = typeof( vArgument );
    switch ( sTypeOf )
    {
        case "string":
            var sArgumentType = sTypeOf;
            break;
        case "object":
            if( vArgument == null )
            {
                var sArgumentType = "null";
            }
            else
            {
                if( vArgument.constructor == Array )
                {
                    var sArgumentType = "array";
                }
            }
    }
    ...my statements...
}

The typeof() operator will return only these values:

"number" "string" "boolean" "function" "undefined" "object"

Even though JS includes the following native object types:

Object, Function, Array, String, Boolean, Number, Math, Date, RegExp, Error, null

we cannot identify all of them with typeof(), nor any other JS operator.

Array, Date, Math, RegExp, Error and null objects are all identified as "object"

The situation gets even worse when we attempt to identify our own custom objects, or DOM elements, DOM collections and DOM text nodes, which are also identified as "object".

Enter dltypeof()

The dltypeof() works just like typeof(), only it returns more meaningful identifiers for objects. Use dltypeof() in your applications, either through an external file, dltypeof.js, or copy and paste the function into your own scripts.

Code Discussion

We will discuss dltypeof(), statement-by-statement in a future article, after we have used it for awhile and added any object types missed on this first go-through.

Script Download

dltypeof() can be downloaded in a zip file, or simply copied from the final page of this article.

Download:   dltypeof.zip


Now let's look at the values dltypeof() returns.



Send a comment or suggestion to Peter Belesis

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: August 23, 2004
Revised: August 23, 2004

URL: http://webreference.com/dhtml/column68/