AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 567


Beyond these well-known reserved words, there are other words that may have
problems under some versions of JavaScript including ECMAScript 4, Jscript.NET, and
JavaScript 2.0. While the words shown in Table A-11 may not actually be reserved in your
browser, they should be avoided just to be safe.
Also, you should be considerate of words that are found in intersections with the language,
such as values in XHTML and CSS, which may create confusion or error. Furthermore, given
that the Window object is the parent space, its properties and methods (for example, location)
should also be considered off limits for user-defined values unless being overridden.

Variables


Variables in JavaScript should be defined using the keyword var and may be defined
multiply and assigned upon definition.

var x;
var a,b,c;
var favNum = 33, favColor, favTech = "Ajax";

Given the loose type nature of JavaScript there is no ability or need in JavaScript 1.7 or
earlier to indicate the type of a variable. Some developers may name their variables to
provide some clue to their expected type, though this does not guarantee the actual value.

var strMyName, objDog, numX, boolLikeSushi;

For better or worse, JavaScript will also define variables upon their first use and put
them in the global space.

z = "I'm alive!"; // z is now defined

Variable Scope


Outside of a function or object, variables are within the global space whether explicitly
defined with var or not. Within a function or object, if the var statement is used, the defined
variable will be local to the construct; without the statement, it will be global. The following
code fragment shows these possibilities:

var global1 = true;
global2 = true;

as event is uint
assert get namespace ulong
decimal include require use

ensure internal sbyte ushort
exclude invariant set

TABLE A-11 Potentially Reserved Words
Free download pdf