Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|
The ECMAScript Language Specification dedicates an entire section (section 11) to Expressions. However, the majority of the section pertains to operators. Operators are a form of an expression. We, at this site, have chosen to discuss Operators separately from Expressions. Therefore, you will find Operators at this link, JavaScript Operators.
On this page, you will find a complete outline with references of section 11 of the ECMAScript Language Specification. The topics under the first sub-section (11.1 Primary Expressions) are addressed in detail on this page. These topics are special forms of expressions. The remaining topics (11.2 through 11.14) are all operator related and we provide a link into the appropriate pages for each operator.
Typically, an expression is any portion of a statement that has a value associated with it. This board definition will encompass many aspects of a statement. The simplest and most direct form of an expression is a literal. The value associated with the literal expression is the literal itself. An identifier is a form of an expression. Operators, when combined with other tokens will form expressions. A simple expression involves a single token such as an identifier or a literal. A compound expression will consist of multiple tokens (like computations). Both simple and compound expressions are typically combined with operators.
More commonly, when we think of an expression, we have in mind a sequence of tokens that are grouped in such a way that together they equate to a single value. Formulas and equations will reduce to a single value.
Section 11 is outlined below. Note that most of section 11 pertains to operators. We provide links to the areas in our site that pertains to each sub-section.
Typically, a method is designed so that it can be accessed by any number of different objects. This is true of both class methods and instance methods. When a method needs to refer to the invoking object, it has the use of the this keyword. All methods have the this unique property.
function rectArea( ) {return this.width * this.height}
The this property provides a reference back to the caller. The this keyword may be used in four separate scenarios.
function Clock(hours, minutes, seconds)
{
this.hr = hours;
this.min = minutes;
this.sec = seconds;
}
The body of the function statement defines three instance properties (hr, min and sec). The this keyword is a reference to the object being instantiated. This gives the constructor access to the object being created. Each object instance of class type Clock that is instantiated via the constructor will receive the properties (hr, min and sec).
// first declare the function
// this function will set the time of a Clock object
function setTime_Clock( hours, minutes, seconds)
{
if ( hours >= 0 && hours < 24)
this.hr = hours;
else
this.hr = 0;
if ( minutes >= 0 && minutes < 60)
this.min = minutes;
else
this.min = 0;
if ( seconds >= 0 && seconds < 60)
this.sec = seconds;
else
this.sec = 0;
}
// now assign the new function to the prototype object of class type Clock
Clock.prototype.setTime = setTime_Clock;
Take another link to This Keyword.
An expression is any portion of a statement that has a value associated with it. How does JS associate a value to an identifier? The result of evaluating an Identifier is always a value of type Reference. Remember the scope chain? To associate a value for an identifier, we first go to the scope chain. We drill down the chain until we "hit" the identifier. The ECMAScript term type Reference relates to combining the name of the call object (or base) from the scope chain where the "hit" occurred. That is, type Reference = base + identifier. If the "hit" occurs in the Global object, JS will return "null" for the base value. The type Reference process is an internal thing. We, as programmers, do not have control over it, but we need to understand how two identifiers with the same name can co-exist and how the appropriate value is returned to us.
Literals are a simple form of a JS expression. The literal value evaluates to the literal token. But it is not that simple. Literal tokens will vary depending on the intended data type. Section 11.1.3, Literal Reference, pertains to primitive data types and regular expressions. Here, we will not replicate material already covered elsewhere. See, Creating Regular Expressions Patterns (Two Approaches) for regular expression literals. And see, Literals of Primitive Data Types for literals of primitive data types.
An array initialize is an expression describing the initialization of an Array object, written in the form of a literal. It is a list of zero or more expressions, each of which represent an array element, enclosed in square brackets. The Array literal and the square bracket operator are covered at the indicated links.
An object initialize is an expression describing the initialization of an Object, written in the form resembling a literal. It is a list of zero or more pairs of property names and associated values enclosed in curly braces. The object literal consists of property specifications, each separated by commas and all enclosed by a set of curly braces. Each property specification consists of the property name followed by a colon and finally the property value.
var aObject = {
fname: "Fritz",
lname: "Fry",
add1: "12345 S. Street",
city: "Town",
state: "CA",
zip: null
};
Expressions can reside inside the grouping operator. The grouping operator is a set of parentheses. The expression inside the grouping operator will evaluate to a single value. The grouping operator can assure proper precedence and associativity for a given operation set. The grouping operator is separate and distinct from the parentheses operator.