比较运算符

比较运算符的期望类型是两个字符串或两个数字,类型转换规则如下:

当操作数为对象时,对象转换为原始值。

之后,如果两个操作数都是字符串,按字母表顺序比较。否则至少有一个操作数不是字符串,两个操作数都转换为数字比较。

比较运算符更偏爱数字,只有两个操作数都是字符串时,才会进行字符串比较。

a>=b 等价于!(a<b),a<=b等价于!(a>b),而与相等(==)和恒等(===)的规则无关。

当操作数中有一个数为NaN时,比较操作总是返回false

in运算符

属性名 in 对象

如果属性名位于对象中,表达式返回true。

instanceof运算符

对象 instanceof 类

如果左操作数不是对象,表达式返回false。如果右操作数不是函数,表达式抛出类型错误异常。

如果对象是类的实例,表达式返回true。

To evaluate the expression o instanceof f, JavaScript evaluates f.prototype, and then looks for that value in the prototype chain of o. If it finds it, then o is an instance of f (or of a superclass of f) and the operator returns true. If f.prototype is not one of the values in the prototype chain of o, then o is not an instance of f and instanceof returns false.

逻辑表达式

逻辑与(&&)

  1. it returns true if and only if both its first operand and its second operand are true. If one or both of these operands is false, it returns false.
  2. If both operands are truthy, the operator returns a truthy value. Otherwise, one or both operands must be falsy, and the operator returns a falsy value.
  3. If the value on the left is falsy, && simply returns the value on the left and does not even evaluate the expression on the right. When the value on the left is truthy, the && operator evaluates and returns the value on the right.

In general, you must be careful whenever you write an expression with side effects (assignments, increments, decrements, or function invocations) on the right-hand side of &&. Whether those side effects occur depends on the value of the left-hand side.

if (a == b) stop(); // Invoke stop() only if a == b

(a == b) && stop(); // This does the same thing

逻辑或(||)

If the value of first operand is truthy, the || operator returns the value of first operand. Otherwise, it evaluates its second operand and returns the value of second operand.

An idiomatic usage of this operator is to select the first truthy value in a set of alternatives:

// If max_width is defined, use that. Otherwise look for a value in

// the preferences object. If that is not defined use a hard-coded constant.

var max = max_width || preferences.max_width || 500;

This idiom is often used in function bodies to supply default values for parameters:

// Copy the properties of o to p, and return p

function copy(o, p) {

    p = p || {}; // If no object passed for p, use a newly created object.

    // function body goes here

}

逻辑非(!)

The ! operator converts its operand to a boolean value before inverting the converted value. This means that ! always returns true or false.

you can convert any value x to its equivalent boolean value by applying this operator twice: !!x

// These two equalities hold for any values of p and q

!(p && q) === !p || !q

!(p || q) === !p && !q

赋值表达式

The = operator expects its left-side operand to be an lvalue: a variable or object property (or array element). It expects its right-side operand to be an arbitrary value of any type. The value of an assignment expression is the value of the right-side operand. As a side effect, the = operator assigns the value on the right to the variable or property on the left so that future references to the variable or property evaluate to the value.

Note that = has very low precedence and parentheses are usually necessary when the value of an assignment is to be used in a larger expression.

The assignment operator has right-to-left associativity, which means that when multiple assignment operators appear in an expression, they are evaluated from right to left.

带操作的赋值运算

As you might expect, the += operator works for numbers or strings.

表达式计算

本节的内容是eval()函数,尽量不使用此函数。

其他运算符

条件运算符(?:)

typeof()

Note that typeof returns “object” if the operand value is null.

typeof may return a string other than "object" for host objects. In practice, however, most host objects in client-side JavaScript have a type of "object".

Because typeof evaluates to "object" for all object and array values other than functions, it is useful only to distinguish objects from other, primitive types.

delete运算符

delete is a unary operator that attempts to delete the object property or array element specified as its operand.

delete is typically used for its property deletion side effect, and not for the value it returns.

You can test for the actual existence of a property with the in operator.

delete expects its operand to be an lvalue. If it is not an lvalue, the operator takes no action and returns true. Otherwise, delete attempts to delete the specified lvalue. delete returns true if it successfully deletes the specified lvalue.

Not all 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. Functions defined with the function statement and declared function parameters cannot be deleted either.

In ECMAScript 5 strict mode, delete raises a SyntaxError if its operand is an unqualified identifier such as a variable, function, or function parameter: it only works when the operand is a property access expression. Strict mode also specifies that delete raises a TypeError if asked to delete any nonconfigurable property.

delete o.x; // Delete a nonexistent property returns true

void运算符

void is a unary operator that appears before its single operand, which may be of any type. This operator evaluates its operand, then discards the value and returns undefined. Since the operand value is discarded, using the void operator makes sense only if the operand has side effects.

逗号操作符(,)

The comma operator is a binary operator whose operands may be of any type. It evaluates its left operand, evaluates its right operand, and then returns the value of the right operand.