|
Page 2 of 2
Lazy evaluation
There is yet another small and subtle point. Notice that if the first value is truthy then it's value is returned and the value of the second term is irrelevant. Javascript takes notice of this fact and saves time by not even bothering to evaluate the second term if it isn't needed. This is more generally called lazy evaluation. For example:
A=1; A=A||test("function called");
function test(a) { alert(a); }
As A is truthy there is no need to evaluate the second term and hence the function test is never called. On the other hand if A=0 or any other falsey value the function is called.
This sort of lazy evaluation can help you avoid runtime errors. The basic idea is that you write something as the first expression that evaluates to truthy when the second expression should not be evaluated. For example, Javascript throws a runtime error if you try to access a method on a null object:
A=null; A.method();
but we can easily check to see if a variable is null using:
!A||A.method();
in this expression !A. i.e. not A, is truthy if A is null and hence we never evaluate A.method if A is indeed null.
You can avoid using the ! (NOT) operator (see later) by expressing the same idea using the && operator as will now be explained.
AND &&
All of the ideas that we have just encountered with respect to the logical Or operator || apply to the And && operator.
The usual truth table for AND i.e. && in Javascript is:
| A |
B |
A&&B |
| F |
F |
F |
| F |
T |
F |
| T |
F |
F |
| T |
T |
T |
which we now have to interpret as truthy and falsey values.
You can also see that you can reinterpret the table in an "active" way and formulate the AND operation as -
if the first expression is falsey then the result is the second expression.
If the first expression is truthy then the result is the first expression.
You could interpret it as saying:
"return the first value if it is false and return the second value otherwise".
Notice that is just the way we work out the logical OR but with the first expression false rather than true.
You should also be able to see the the AND operation
A&&B;
is equivalent to the if statement:
if(A){B};
If you think about this for a moment you should be able to see that !A&&B is "almost" the same as A||B in that both give the value B when A is false.
However it isn't identical because when A is true !A&&B gives the logical opposite of A||B that is !A&&B is false but A||B is true if A is true. The exact equivalence is given by one of De Morgan's laws of logic and it reveals that !A&&B is the same as !(A||!B).
You can use the AND operator in much the same way as the OR operator and it too uses lazy evaluation - that is if A is false then B in A&&B is not evaluated.
This enables us to write the protection against a runtime error given earlier as:
A=null; A&&A.method();
and avoid the use of a NOT !.
The NOT !
Finally we come to the NOT ! operator. This works in the same way as the other logical operators but it only has a single operand and also works with truthy and falsey values:
It is also worth knowing that it converts all truthy and falsey values into false and true i.e. proper Boolean values. So !null is true, !0 is true and so on.
For example:
A=null; if(!A) alert("A is null");
will flag the fact that A doesn't exist.
Ternary expression
You may have encountered the Javascript ternary expression before but always regarded it as something special and not part of the whole logic of the language but seen in the light of the way AND and OR are implemented it makes a lot more sense.
- AIIB returns the second expression if A is false
- A&&B returns the second expression if A is true
- A?B:C returns B is A is true and C is A is false
In terms of a truth table this too is just a logical function but one with three inputs:
| A |
B |
C |
A?B:C |
| F |
F |
F |
F |
| F |
F
|
T |
T |
| F |
T |
F |
F |
| F |
T |
T |
T |
| T |
F |
F |
F |
| T |
F |
T |
F |
| T |
T |
F |
T |
| T |
T |
T |
T |
In this case it is the "active" interpretation of the logic that makes more sense than the "passive" truth table.
We have already noted that the AND && and OR || operators are the equivalent of if statements. That is:
A&&B;
and
A||B;
are the same as
if(A){B}
and
if(!A){B}
It is interesting to notice that the ternary expression is also the equivalent of an if..else statement. That is:
A?B:C
is the same as
if(A) { B} else {C};
This also means that the if and the if..else statements are just logical functions with the truth table given earlier. This is not the way we usually think about them.
Active logic and lazy evaluation make a great deal of sense out of some aspects of Javascript that might otherwise look strange and arbitrary.
Task.js Asynchronous Tasks In JavaScript
There are some interesting things going on the latest version of JavaScript and already some new uses are being found for them. Task.js uses the new generator facility to build an asynchronous task fa [ ... ]
|
jQuery UI Custom Control - Widget Factory
So you have been using jQuery and jQuery UI but you have a few custom JavaScript controls that don't work in the same way. The solution is to create a jQuery UI custom control so that you can use ever [ ... ]
| | Other Articles |
<ASIN:0470684143>
<ASIN:0470684143>
<ASIN:0137054890>
<ASIN:0470526912>
<ASIN:1449381871>
<ASIN:1430230541>
<ASIN:0321683919>
|