Professional JavaScript


[next]

Professional JavaScript

Advanced Language Features

It doesn't take much practice to be able to write some basic JavaScript. Chapters 1 and 2, combined with some information on a JavaScript-embedded host application such as a Web browser, (like that in Chapter 4) are enough to get going. If you're impatient to get on with your Web application, come back to this chapter later.

For those of you still reading, the JavaScript core language has some pretty fancy features we've not covered yet, and with the upcoming "ECMAScript 2" due for release in late 1999, more features of the language will be standardized. If you feel the need to tackle heavier-duty scripting tasks either inside Web browsers or outside of them, then this chapter will give you the ammunition to do the job.

Just like many other modern languages, JavaScript scripts can be modeled in an "object-oriented" way. Recent innovations by Microsoft and Netscape introduce mechanisms to support generic "exception handling," "namespace management" and "regular expressions." In the following sections you'll see the language support available and some analysis of how well supported these grown-up features are.

Before jumping into these concepts, you first need to understand some JavaScript language plumbing. So here's that first.

Prototype and Scope Chains

JavaScript is an interpreted language. If it were a compiled language, every named object, property and method used in the source code could be nailed down during the compilation phase, removing all doubt as to what was what. Then, when the compiled program was run, the program or script could blindly charge ahead, confident that everything was organized perfectly in advance by the compiler. An interpreter requires a more sophisticated approach. At any time a newly named variable can spontaneously appear, and the interpreter has the job of managing and tracking it. The section on eval() at the end of this chapter shows how unpredictable this process can be.

In this next example, just accept for now that the strings in line 1, whose contents look suspiciously like a bit of JavaScript, do in fact get turned into runnable script somehow. The question is, how does JavaScript know to display the new_price variable in this (or in any other) script: eval('var new_pr' + 'ice = 99.95; '); // no variables – just a string document.write(new_price); // shows 99.95

Clearly, an interpreter can't look through the script and foresee every possible string that might contain valid JavaScript statements in advance. It must have some additional method of tracking what properties belong to what objects. This section explains the mechanisms at work.

Simplifications Revisited

JavaScript allows objects to be constructed as described in Chapter 2, using constructor functions and the prototype property. In that discussion, the implications of the prototype property were glossed over to a degree. In Chapter 17, when we tackle debugging, one of the common problems discussed is that of forgetting that the "current object" within a Web browser's form element's event handler is different to the usual object, which is the window object. In that discussion, just what a "current object" might be is glossed over to a degree as well.

Recall that methods, variables and objects are all just properties of some other object, even if that other object is merely the global object. A property name is also called an identifier. When an identifier for a property (variables, objects, methods and functions are all properties too, recall) appears in a JavaScript program, two questions need answering in order to find out what it stands for:

The answers lie in the prototype mechanism in the first case, and in the scope chain mechanism in the second.


[next]
and
Created: February 2, 2001
Revised: February 2, 2001

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