Language Essentials for Experienced Programmers
| This article is brought to you by Hungry Minds, Inc. publisher of Danny Goodman's JavaScript Bible |
In this article, experienced programmers can read the highlights about the core JavaScript language in terms that may not make complete sense to those with limited or no scripting experience. This article is especially for you if you found tutorials on JavaScript rudimentary. Here, then, is the quick tour of the essential issues surrounding the core JavaScript language.
JavaScript is a scripting language. The language is intended for use in an existing host environment (for example, a Web browser) that exposes objects whose properties and behaviors are controllable via statements written in the language. Scripts execute within the context of the host environment. The host environment controls what, if any, external environmental objects may be addressed by language statements running in the host environment. For security and privacy reasons, Web browsers generally afford little or no direct access via JavaScript to browser preferences, the operating system, or other programs beyond the scope of the browser. The exception to this rule is that modern browsers allow deeper client access (with the user’s permission) through trust mechanisms such as signed scripts (Netscape) or trusted ActiveX controls (Microsoft).
JavaScript is object-based. Although JavaScript exhibits many syntactic parallels with the Java language, JavaScript is not as pervasively object-oriented as Java. The core language includes several built-in static objects from which working objects are generated. Objects are created via a call to a constructor function for any of the built-in objects plus the new operator. For example, the following expression generates a String object and returns a reference to that object:
new String("Hello")
Table 13-1 lists the built-in objects with which scripters come in contact.
Table 13-1 JavaScript Built-in Objects
|
Array1 |
Boolean |
Date |
Error2 |
|
EvalError2 |
Function1 |
Math |
Number1 |
|
Object1 |
RangeError2 |
ReferenceError2 |
RegExp3 |
|
String1 |
SyntaxError2 |
TypeError2 |
URIError2 |
1Although defined in ECMA Level 1, was first available in NN3 and IE3/J2
2Defined in ECMA Level 3; implemented in NN6
3Defined in ECMA Level 3; implemented fully in NN4, partially in IE4
JavaScript is loosely typed. Variables, arrays, and function return values are not defined to be of any particular data type. In fact, an initialized variable can hold different data type values in subsequent script statements (obviously not good practice, but possible nonetheless). Similarly, an array may contain values of multiple types. The range of built-in data types is intentionally limited:
Boolean (true or false)
Null
Number (double-precision 64-bit format IEEE 734 value)
Object (encompassing the Array object)
String
Undefined
The host environment defines global scope. Web browsers traditionally define a browser window or frame to be the global context for script statements. When a document unloads, all global variables defined by that document are destroyed.
JavaScript variables have either global or local scope. A global variable in a Web browser is typically initialized in var statements that execute as the document loads. All statements in that document can read or write that global variable. A local variable is initialized inside a function (also with the var operator). Only statements inside that function may access that local variable.
Scripts sometimes access JavaScript static object properties and methods. Some static objects encourage direct access to their properties or methods. For example, all properties of the Math object act as constant values (for example, Math.PI).
You can add properties or methods to working objects at will. To add a property to an object, simply assign a value of any type to it. For example, to add an author property to a string object named myText, use:
myText.author = "Jane"
Assign a function reference to an object property to give that object a new method:
// function definition
function doSpecial(arg1) {
// statements
}
// assign function reference to method name
myObj.handleSpecial = doSpecial
...
// invoke method
myObj.handleSpecial(argValue)
Inside the function definition, the this keyword refers to the object that owns the method.
JavaScript objects employ prototype-based inheritance. All object constructors create working objects whose properties and methods inherit the properties and methods defined for the prototype of that object. Starting with NN3 and IE3/J2, scripts can add and delete custom properties and/or methods associated with the static object’s prototype so that new working objects inherit the current state of the prototype. Scripts can freely override prototype property values or assign \tdifferent functions to prototype methods in a working object if desired without affecting the static object prototype. But if inherited properties or methods are not modified in the current working object, any changes to the static object’s prototype are reflected in the working object. (The mechanism is that a reference to an object’s property works its way up the prototype inheritance chain to find a match to the property name.)
JavaScript includes a large set of operators. You can find most operators that you are accustomed to working with in other languages.
JavaScript provides typical control structures. All versions of JavaScript offer if, if-else, for, and while constructions. JavaScript 1.3 (NN4+ and IE4+) also add do-while and switch constructions. Iteration constructions provide break and continue statements to modify control structure execution.
JavaScript functions may or may not return a value. There is only one kind of JavaScript function. A value is returned only if the function includes a return keyword followed by the value to be returned. Return values can be of any data type.
JavaScript functions cannot be overloaded. A JavaScript function accepts zero or more arguments, regardless of the number of parameter variables defined for the function. All arguments are automatically assigned to the arguments array, which is a property of a function object. Parameter variable data types are not predefined.
Values are passed "by reference" and "by value." An object passed to a function is actually a reference to that object, offering full read/write access to properties and methods of that object. But other types of values (including object properties) are passed by value, with no reference chain to the original object. Thus, the following nonsense fragment empties the text box when the onChange event fires:
function emptyMe(arg1) {
arg1.value = ""
}
...
<INPUT TYPE="text" VALUE="Howdy" onChange="emptyMe(this)">
But in the following version, nothing happens to the text box:
function emptyMe(arg1) {
arg1 = ""
}
...
<INPUT TYPE="text" VALUE="Howdy" onChange="emptyMe(this.value)">
The local variable (arg1) simply changes from "Howdy" to an empty string.
Error trapping techniques depend on JavaScript version. There is no error trapping in NN2 or IE3. Error trapping in NN3, NN4, and IE4 is event-driven in the Web browser object model. JavaScript, as implemented in IE5 and NN6, supports \ttry-catch and throw statements, as well as built-in error objects that are not dependent on the host environment.
Memory management is not under script control. The host environment manages memory allocation, including garbage collection. Different browsers may handle memory in different ways.
White space (other than a line terminator) is insignificant. Space and tab characters may separate lexical units (for example, keywords, identifiers, and so on).
A line terminator is usually treated as a statement delimiter. Except in very rare constructions, JavaScript parsers automatically insert the semicolon statement delimiter whenever they encounter one or more line terminators (for example, carriage returns or line feeds). A semicolon delimiter is required between two statements on the same physical line of source code. Moreover, string literals may not have carriage returns in their source code (but an escaped newline character (\n) may be a part of the string).
About the Author
Danny Goodman is the author of numerous critcally acclaimed and best-selling books. In addition to his consulting and designing in the San Francisco area, he is also a renowned authority and expert teacher of computer scripting languages.
|
This article is brought to you by Hungry Minds, Inc.
publisher of Danny Goodman's JavaScript Bible © Copyright Hungry Minds, All Rights Reserved |
|


