Note Three: studying JavaScript: The Definitive Guide, 4th Edition

Chapter 5. Expressions and Operators
1. Arithmetic Operators :
Addition (+) , Subtraction (-) , Multiplication (*) , Division (/) ,Modulo (%) , Unary minus (-) , Unary plus (+) , Increment (++) , Decrement (--)
The + operator adds numeric operands or concatenates string operands. If one operand is a string, the other is converted to a string and the two strings are then concatenated.

Division by zero yields positive or negative infinity, while 0/0 evaluates to NaN.

2. Equality Operators
1) ===
If the two values have different types, they are not identical.
If both values are numbers and have the same value, they are identical, unless either or both values are NaN, in which case they are not identical.
If both values are null or both values are undefined, they are identical.

2) ==
If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.
If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality:
<1> If one value is null and the other is undefined, they are equal.
<2>If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
<3>If either value is true, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again.
<4>If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.

3) please remeber followings:
null vs undefined :
null == null
null === null
undefined == undefined
undefined === undefined
undefined == null
undefined !== null

NaN :
NaN != NaN
NaN !== NaN

String :
'abc' == 'abc'
'abc' === 'abc'

boolean :
1 == true
0 == false
2 != true
2 != false
'1' == true
'0' == false

3. Relational Operators
1) Comparison Operators : Less than (<) , Greater than (>) , Less than or equal (<=) , Greater than or equal (>=)
If both operands are numbers, or if both convert to numbers, they are compared numerically.
If both operands are strings or convert to strings, they are compared as strings.
The <= (less-than-or-equal) and >= (greater-than-or-equal) operators do not rely on the equality or identity operators for determining whether two values are "equal." Instead, the less-than-or-equal operator is simply defined as "not greater than,"
when either operand is (or converts to) NaN, it will return false.

Please remain in mind the followings:
10 > 2 = true
'10' > 2 = true
10 > '2' = true
'10' > '2' = false
'Z' > 'a' = false

2) The in Operator
It evaluates to true if the lefthand value is the name of a property of the righthand object. For example:

var point = { x:1, y:1 };        // Define an object
var has_x_coord = "x" in point;  // Evaluates to true
var has_y_coord = "y" in point;  // Evaluates to true
var has_z_coord = "z" in point;  // Evaluates to false; not a 3-D point
var ts = "toString" in point;    // Inherited property; evaluates to true

3) The instanceof Operator
The operator evaluates to true if the lefthand object is an instance of the righthand class and evaluates to false otherwise.

var d = new Date();   // Create a new object with the Date(  ) constructor
d instanceof Date;    // Evaluates to true; d was created with Date(  )
d instanceof Object;  // Evaluates to true; all objects are instances of Object
d instanceof Number;  // Evaluates to false; d is not a Number object
var a = [1, 2, 3];    // Create an array with array literal syntax
a instanceof Array;   // Evaluates to true; a is an array
a instanceof Object;  // Evaluates to true; all arrays are objects
a instanceof RegExp;  // Evaluates to false; arrays are not regular expressions

4. String Operators
The + operator is a special one -- it gives priority to string operands over numeric operands. As noted earlier, if either operand to + is a string (or an object), the other operand is converted to a string (or both operands are converted to strings) and concatenated, rather than added.

5. Logical Operators
Logical AND (&&) , Logical OR (||) , Logical NOT (!)
1) Logical AND (&&)
If the value of this expression can be converted to false (for example, if the left operand evaluates to null, 0, "", or undefined), the operator returns the value of the lefthand expression. depending on the value of the lefthand expression, this operator may or may not evaluate the righthand expression.

6. Assignment Operators
The assignment operator has right-to-left associativity. Thus, you can write code like this to assign a single value to multiple variables:

i = j = k = 0;

7. Miscellaneous Operators
1) The typeof Operator
The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value. It evaluates to "object" for objects, arrays, and (surprisingly) null. It evaluates to "function" for function operands and to "undefined" if the operand is undefined.

2) The delete Operator
Not all variables and properties can be deleted: some built-in core and client-side properties are immune from deletion, and user-defined variables declared with the var statement cannot be deleted. If delete is invoked on a nonexistent property, it returns true.

var o = {x:1, y:2};  // Define a variable; initialize it to an object
delete o.x;          // Delete one of the object properties; returns true
typeof o.x;          // Property does not exist; returns "undefined"
delete o.x;          // Delete a nonexistent property; returns true
delete o;            // Can't delete a declared variable; returns false
delete 1;            // Can't delete an integer; returns true
x = 1;               // Implicitly declare a variable without var keyword
delete x;            // Can delete this kind of variable; returns true
x;                   // Runtime error: x is not defined

It is important to understand that delete affects only properties, not objects referred to by those properties. Consider the following code:
var my = new Object(  );    // Create an object named "my"
my.hire = new Date(  );     // my.hire refers to a Date object
my.fire = my.hire;          // my.fire refers to the same object
delete my.hire;             // hire property is deleted; returns true
document.write(my.fire);    // But my.fire still refers to the Date object

3) The void Operator
void is a unary operator that appears before its single operand, which may be of any type. The purpose of this operator is an unusual one: it discards its operand value and returns undefined. The most common use for this operator is in a client-side javascript: URL, where it allows you to evaluate an expression for its side effects without the browser displaying the value of the evaluated expression.

if you can't understand the void operator, please test the following code in your browser.

<a href="javascript:void window.open(  );">Open New Window with void</a>
<a href="javascript:window.open(  );">Open New Window</a>

 

posted on 2006-02-11 21:51  microsheen  阅读(954)  评论(0编辑  收藏  举报