Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Expressions

The term expression has been mentioned dozens of times throughout this chapter. An expression is any valid set of literals, variables, operators, and other expressions that evaluates to a single value. The value may be a number, a string, or a Boolean value. Conceptually, there are two types of expressions:

  • Those that assign a value to a variable (or another data structure)
  • Those that have a value

The following are expressions:

a = "Shiran"
"Netscape"
false
b = true

The first type of expression is a bit more difficult to understand. In such expressions, an assignment is performed. The entire specification, including both operands of the assignment operator and the operator itself, is evaluated to the assigned value (“Shiran”). Consider the following statement:

document.write(x = "Israel")

This statement prints Israel, because the whole expression evaluates to the assigned value. The next section discusses side effects which take advantage of such expressions.

Side Effects

A side effect is an operation performed in addition to the main one. Take a look at the following statements:

number = 3
answer = ++number

The first line is a simple assignment statement. The second line is a bit more complicated. It performs two actions in the following order:

  • Increments number (side effect)
  • Assigns the value of number (4) to answer (main operation)

Remembering the order in which the actions take place is not necessary. You could break down the side effect and use two different statements instead:

number = 3
number++
answer = number

This set of statements is easy to follow. The only “advantage” of the first method is that the code is compact. Compact code is only a holdover from the early days of computing, when storage was expensive, and programmers used various compacting techniques to save disk space. The situation is different today, because the number one rule in programming is to keep programs clear and simple. It comes even before efficiency, especially in simple programs like the ones written in JavaScript (you will not find any 500,000-line scripts in this advanced book).

Other side effects are not related to the increment and decrement operators. Consider the following JavaScript statement:

document.write(myName = "Yehuda")

You already know that this statement prints Yehuda. The assignment operation also takes place in this statement, so myName holds the value Yehuda after the statement. The assignment operation is the side effect, and the printing action is the main action. The preceding statement should be split into two separate statements:

myName = "Yehuda"
document.write(myName)

Here are some more “Morse” statements you should avoid (they will keep you busy for a while...):

number = 0
answer = (number++ – 2) * (--number – 1)
document.write(number = answer++)

This set of statements prints 2. Let’s analyze it:

1.
1.1  number is assigned the value 0.
2.  
2.1.  (number++ – 2) is evaluated to –2.
2.2.  number is incremented to 1.
2.3.  number decrements to 0.
2.4.  (--number – 1) evaluates to –1.
2.5.  answer is assigned 2 (because (–1) * (–2) yields 2).
3.  
3.1.  number is assigned the value of answer (2).
3.2.  The value of number is printed.
3.3.  answer increments to 3.

The final result:

  • number holds the value 2.
  • answer holds the value 3.
  • The number 2 is printed to the document.

The following script is longer, but it is definitely better:

number = 0
answer = number – 2
number++
number--
answer *= number – 1
number = answer
document.write(number)
number = amswer
answer++

Summary

In this chapter we discussed JavaScript’s wealth of operators. Some of them are extremely useful, while others are not used at all. We also discussed various expression forms. In addition, we played around with some side effects. Never use side effects in your programs, because they are hard to follow and difficult to understand. The next chapter will discuss control structures and will show how useful the wide variety of operators is.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us