spacer

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

home / programming / javascript / professional / chap3 / 4 1234
[previous] [next]
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Professional JavaScript

eval() Concepts

Interpreted languages like JavaScript have a very useful peculiarity that compiled languages lack. Because the interpreter exists as part of the program that runs the script, the possibility exists that more interpretation of JavaScript statements can occur after the script has been read and started. We might call this dynamic or run-time evaluation to emphasize that it happens after the script gets going. Sometimes this might be called piggyback embedding.

Simple Dynamic Evaluation – Maps

Chapter 2 discussed how objects and arrays have interchangeable syntax. Recall that these two statements are equivalent:


object.property = "stuff"
array["property"] = "stuff"

Because the argument supplied to the array is an expression, it's possible to do handy things like this:


var all_cars = new CarCollection;
var car;
while ( (car=get_car_name()) != "" )
  all_cars[car.toLowerCase()] = get_registration(car);
 

In the last line, the variable car is used to create the property names of the all_cars object so that one property matches each car in the collection. Then each of those properties is set to a related value for that car – in this case the registration number. So if these collectable cars existed:


Model T Ford	A234
Goggomobile	MCI 223
Austin Lancer	JG 33

Then the properties of the all_cars object would be "model t ford", "goggomobile" and "austin lancer". Later we could refer to those properties as follows to dig out the related information:


var x = "model t ford";
all_cars[x];                  // "A234"
all_cars.goggomobile;         // "MCI 233"
all_cars["austin lancer"];    // "JG 33"

Effectively, the properties of the object depend on the data available, and that might not be known until runtime (if the user enters it, for example).

This ability to interchange between data and variable names is a powerful and common feature of scripting languages. In particular, this example shows a very commonly used structure dubbed alternatively a map, mapping, associative array, hash, or merely dictionary. The point is that it lets you get at a data value (a property's value) based on a unique key value (the property name), and it lets you keep a bunch of such pairs neatly together. It is a simple form of unordered index. Mathematicians say "There is a mapping from the set of collectable cars to the set of registration numbers for those cars". Whatever.

Note that in this example we'd be in trouble if a second Model T Ford turned up. That second car would have the same property name as the first and the system breaks down. That's why this approach is nearly always used for data that has a set of unique values only. Plenty of data is like that.

home / programming / javascript / professional / chap3 / 4 1234
[previous] [next]

Copyright 1999 (1st Edition) and 2001 (2nd Edition) Wrox Press Ltd. and

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: February 12, 2001
Revised: February 16, 2001


URL: http://webreference.com/programming/javascript/professional/chap3/