11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> last operator that is associated with objects is the parentheses operator. This operator is<br />

used to invoke an object‘s method just as it invokes functions. For example, we have already<br />

seen the Document object‘s write() method:<br />

document.write("Hello from <strong>JavaScript</strong>");<br />

In this case, we pass a single parameter, the string "Hello from <strong>JavaScript</strong>", to the write method<br />

so that it is printed to the HTML document. In general, we can invoke arbitrary object methods<br />

as follows:<br />

objectname.methodname(optional parameters)<br />

Operator Precedence and Associativity<br />

Operators have a predefined order of precedence, that is, order in which they are evaluated in<br />

an expression. This is particularly obvious with arithmetic operators and is similar to the<br />

evaluation of equations in algebra, where multiplication and division have higher precedence<br />

over addition and subtraction. For example, the result of<br />

alert(2 + 3 * 2);<br />

will be 8 because the multiplication is performed before the addition. We see that multiplication<br />

has higher precedence than addition. Using parentheses, we can group expressions and force<br />

their evaluation in an order of our choice. Parenthesized expressions are evaluated first. For<br />

example,<br />

alert((2 + 3) * 2);<br />

will display 10.<br />

Of course, expression evaluation is also influenced by the operator associativity. Associativity<br />

essentially means the ―direction‖ in which an expression containing an operator is evaluated.<br />

For example, consider the following combination of addition and string concatenation<br />

operations:<br />

alert(5 + 6 + "Hello");<br />

<strong>The</strong> result will be the string "11Hello" rather than "56Hello." Even though the two instances of +<br />

would appear to have the same precedence, the + operator is ―left associative,‖ meaning that it<br />

is evaluated left to right, so the numeric addition is performed first. Conversely, in this example,<br />

var y;<br />

var x = y = 10 * 10;<br />

the multiplication is performed first because assignment (=) is ―right associative.‖ <strong>The</strong> result is<br />

that 100 is computed, then assigned to y, and only then assigned to x.<br />

<strong>The</strong> precedence and associativity of the various operators in <strong>JavaScript</strong> is presented in Table<br />

4-9. Note that by computer science tradition, precedence is indicated by a number, with lower<br />

numbers indicating higher precedence.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!