SPS Home > Dgreath > JavaScript > Identifiers |
JAVASCRIPT IDENTIFIERS |
---|
The following information describes typical JavaScript nomenclature as a guide to consistent
identifier usage. All JavaScript identifiers must conform to the following
rules:
For example, |
JAVASCRIPT CLASSES |
---|
JavaScript class names always begin with an uppercase letter and are expressed as a single descriptive noun. (ex. the Object, Date, Math, Number, String, Boolean, Array, Function classes). Classes are not executable, but rather are prototypes used as the the foundation of objects. |
JAVASCRIPT OBJECTS |
---|
JavaScript object (an instance of a Class) names always begin with an lowercase
letter
and are expressed as a descriptive noun or noun phrase. (ex. the screen, window, document, navigator, location objects).
A JavaScript object is essentially a collection of related properties and
methods and is an instance of a class. Many commonly used JavaScript objects are
instances of classes provided by the JavaScript runtime engine (the Document
Object Model or DOM) contained in the
web browser. Since a JavaScript object is actually an array of properties, the formal notation is: objectName["propertyName"] for properties, andobjectName["methodName"](arguments) for methods.objectName.propertyName for properties, andobjectName.methodName(arguments) for methods.Note, however, the five built- in JavaScript functions (Boolean, Date, Number, Object, String) deviate from the general rule and have the initial letter capitalized to signify they are top level functions. These functions are used to convert arguments into datatypes and, while derived from the similarly named classes, are different entities. |
FUNCTION PROPERTIES |
---|
Function property names always begin with an lowercase letter
and are expressed as a noun. (ex. screen.availHeight, screen.availWidth ). The use of camel casing (capitalizing the
first letter of each word except for the first) is quite normal. Properties
never stand alone and are either linked to their object name or to the special this
synonym.
|
FUNCTION METHODS |
---|
Function methods always begin with an lowercase letter
and are expressed as a verb or verb phrase (ex. window.open() , window.close()
methods of the window object)
followed by a list of values enclosed in parenthesis. When calling the function, these values
are its arguments, while in the context of the function, they are its external parameters.
The use of camel casing is acceptable.
A function's methods are a property of that function and thus adhere to the
practice guiding the naming of properties. Methods
never stand alone and are either linked to their object name or to the special this
synonym.
|
JAVASCRIPT VARIABLES |
---|
JavaScript variable names are always expressed in lower case and should be descriptive of the value contained within. The datatype of the variable is determined by the type of data with which it is initialized. Avoid the use of systems or application Hungarian notation. While that method has been popular in the strongly typed programming languages such as C++ and Java, it can be problematic in the loosely typed arena of JavaScript. Variables can be either of the primitive datatypes (Boolean, Number, String) or complex (Object, Array, Date, Function, Error, RegExp). The properties of a primitive variable are its name, type, and value. |
JAVASCRIPT STATEMENTS |
---|
JavaScript statements are always expressed in lower case. Therefore, the statement if (a ==
b){a = 4}else {a =5 } is valid while the statement IF (a == b){a =
4}ELSE {a = 5} is
invalid.
|
RESERVED WORDS | ||
---|---|---|
The following words are reserved and may not be used for identifiers: | ||
abstract (2) | final (D) | protected (2) |
as (1) | finally (S) | public (1) |
boolean (D) | float (D) | return (S) |
break (S) | for (S) | short (D) |
byte (D) | function (S) | static (1) |
case (S) | goto (2) | super (1) |
catch (S) | if (S) | switch (S) |
char (D) | implements (2) | synchronized (2) |
class (1) | import (1) | this (C) |
continue (S) | in (O) | throw (S) |
const (1) | instanceof (O) | throws (2) |
debugger (2) | int (D) | transient (2) |
default (S) | interface (1) | true (V) |
delete (S) | is (1) | try (S) |
do (S) | long (D) | typeof (O) |
double (D) | namespace (1) | use (1) |
else (S) | native (2) | var (S) |
enum (2) | new (S) | void (O) |
export (1) | null (V) | volatile (2) |
extends (1) | package (1) | while (S) |
false (V) | private (1) | with (S) |
Unless noted, these keywords are reserved in all implementations of JavaScript (1) Implemented in JavaScript 2.0 (2) Reserved for future implementation beyond JavaScript 2.0 (S) JavaScript statement (O) JavaScript operator (D) JavaScript datatype (kept on the list for backwards compatibility as JS is no longer typed) (V) JavaScript defined value (C) Special identifier used in class creation |
GLOBAL NAMES | |
---|---|
The following object names should be used with caution. JavaScript allow the user to overwrite the built in objects and their properties and methods in cases where the normal construct doesn't work as needed. Therefore, if these names are used, the normal built in objects will no longer function. | |
Object Name | Description |
anchors | Synonym for document.anchors[] array |
forms | Synonym for document.forms[] array |
frames | Synonym for window.frames[] array |
images | Synonym for document.images[] array |
links | Synonym for document.links[] array |
applets | Synonym for document.applets[] array |
objects | Synonym for document.objects[] array |
mimeTypes | Synonym for navigator.mimeTypes[] array |
plugins | Synonym for navigator.plugins[] array |
Array() | The Array class |
Boolean() | The Boolean class and global top level function |
Date() | The Date class and global top level function |
Error() | The Error class |
Function() | The Function class |
Math() | The Math class |
Number() | The Number class and global top level function |
Object() | The Object class and global top level function |
RegExp() | The RegExp class |
String() | The String class and global top level function |
Infinity | The global infinity property |
NaN | The global Not a Number property |
undefined | The global undefined property |
document | The DOM document object |
screen | The DOM screen object |
window | The DOM window object |
navigator | The DOM navigator object |
history | The DOM history object |
location | The DOM location object |
BOOLEAN VALUES |
---|
The two Boolean values of 'true' and 'false' are synonyms for '1' and '0' respectively and may be interchanged freely. Note that 'TRUE' and 'FALSE' are undefined and will always throw an error. |
NUMERICAL LITERALS |
---|
A numerical literal is not a Number object and has neither the properties nor methods of
Number
datatypes although converting
a numeric literal into an object is quite simple with the Number() function. A
numeric literal consists of the characters 0-9, and the decimal
point " . " plus sign " + " and minus sign " - ".
|
STRING LITERALS |
---|
A string literal is not a String object and has neither the properties nor methods of String
datatypes although converting
a string literal into an object is quite simple with the String() function. A string literal principally consists of the alphanumeric characters a-z, A-Z, 0-9, and the
space " ".
In addition, the following escaped characters can be embedded:
~ ! @ # $ % ^ * - + , . ? / | = ; : _ ` ( ) [ ] { } < > ) are
acceptable as well.
|