ECMA-262 Core: JavaScript Expressions



Navigation Aids -- This Page   Navigation Aids -- This Topic   Navigation Aids -- This Site








Introduction And Review

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.



General Discussion

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.



Complete Outline With References for Section 11



The this Keyword - 11.1.1

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.

  1. A User Defined Constructor: As a review, user defined classes will need a special method called a constructor. Constructors methods are used along with the new operator to create (instantiate) objects of the custom class type. The name given the function that represents the constructor also determines the name of the class. Our class in the following code is "Clock". 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).
  2. An instance method: We use the term "method" to indicate that the purpose of the function is to operate on an object. Here we will define a method for the Prototype Object of class type Clock. Recall that the Prototype object is the JS way of facilitating inheritance. // 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;
    The function "setTime_Clock" has been assigned to the prototype object to become the new instance method "setTime" of our Clock class type. The this keyword is a reference to the object instance that is accessing the instance method.
  3. A Stand Alone Function: Typically we do not use the this keyword is a traditional function statement declaration (FSD). Recall that an FSD is declared in top level code (inside <script> tags that are inside <head> tags). All members declared at top level code belong to the Global object. Global object members are accessed directly by name. Therefore, there is no real use for the this keyword with an FSD. But, if you use it, it will reference the Global object.
  4. Class Methods: Because class methods are not invoked through an object, the this keyword has little meaning. Like with the FSD, class methods are global. They behave like functions that happen to be invoked through a class.

Take another link to This Keyword.



Identifier Reference - 11.1.2

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.



Literal Reference - 11.1.3

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.



Array Initialize - 11.1.4

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.



Object Initialize - 11.1.5

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
};



The Grouping Operator - 11.1.6

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.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson