阅读(2385) (2)

表达式与运算符

2017-07-07 17:34:57 更新

表达式

表达式为 JavaScript 的短语可执行并生成值。

1.7 // 字面量
"1.7"
var a = 1;
var b = '2';
var c = (1.7 + a) * '3' - b

运算符

  • 算数运算符 (+ - * / %)
  • 关系运算符 (> < == != >= <= === !==)
  • 逻辑运算符 (! && ||)
  • 位运算符 (& | ^ ~ << >>)
  • 负值运算符 (=)
  • 条件运算符 (?:)
  • 逗号运算符 (,)
  • 对象运算符 (new delete . [] instanceof)

=== 全等符号

全等运算符用于盘对左右两边的对象或值是否类型相同且值相等。

伪代码拆解

function totalEqual(a, b) {
  if (a 和 b 类型相同) {
    if (a 和 b 是引用类型) {
      if (a 和 b 是同一引用)
        return true;
      else
        return false;
    } else { // 值类型
      if (a 和 b 值相等)
        return true;
      else
        return false;
    }
  } else {
    return false;
  }
}

例子

var a = "123";
var b = "123";
var c = "4";
var aObj = new String("123");
var bObj = new String("123");
var cObj = aObj;

a === aObj      // false
aObj === bObj   // false
aObj === cObj   // true
a === b         // true
a === c         // false

==

== 用于判断操作符两边的对象或值是否相等。

伪代码拆解

function equal(a, b) {
  if (a 和 b 类型相同) {
    return a === b;
  } else { // 类型不同
    return Number(a) === Number(b); // 优先转换数值类型
  }
}

例子

"99" == 99; // true
new String("99") == 99; // true
true == 1; // true
false == 0; // true
'\n\n\n' == // true

例外规则

  • null == undefined 结果为真 true
  • 在有 null/undefined 参与的 == 运算是不进行隐式转换。
0 == null; // false
null == false; // false
"undefined" == undefined; // false

! 取反

!x 用于表达 x 表达式的运行结果转换成布尔值(Boolean)之后取反的结果。!!x 则表示取 x 表达式的运行结果的布尔值。

var obj = {};
var a = !obj // false;
var a = !!obj // true;

&& 逻辑与

x && y 如果 x 表达式的运行交过转换成 Boolean 值为 false 则不运行表达式 y 而直接返回 x 表达式的运行结果。相反,如果 x 表达式的运行交过转换成 Boolean 值为 true 则运行表达式 y 并返回 y 表达式的运行结果。

伪代码拆解

var ret = null;
if (!!(ret = x)) {
  return y;
} else {
  return ret;
}

例子

var a = 0 && (function(){return 1 + 1;})(); // 0
var b = 1 && (function(){return 1 + 1;})(); // 2

|| 逻辑或

x || y 如果 x 表达式的运行结果转换为 Boolean 值为 true,则不运行 表达式 y 而直接返回表达式 x 的运算结果。(与 && 方式相反)

伪代码拆解

var ret = null;
if (!!(ret = x)) {
  return ret;
} else {
  return y;
}

例子

var a = 0 || (function(){return 1 + 1;})(); // 2
var b = 1 || (function(){return 1 + 1;})(); // 1

元算符优先级(Operator Precedence)

  • + - * / 高于 &&
  • * / 高于 + -
  • && 高于 ?:
  • () 内优先级高于之外

NOTE:和数学上的算术优先级类似,同级从左到右计算。如有疑问加上 () 既可解决优先级问题。

PrecedenceOperator typeAssociativityIndividual operators
19Groupingn/a( … )
18Member Accessleft-to-right… . …
Computed Member Accessleft-to-right… [ … ]
new (with argument list)n/anew … ( … )
17Function Callleft-to-right… ( … )
new (without argument list)right-to-leftnew …
16Postfix Incrementn/a… ++
Postfix Decrementn/a… --
15Logical NOTright-to-left! …
Bitwise NOTright-to-left~ …
Unary Plusright-to-left+ …
Unary Negationright-to-left- …
Prefix Incrementright-to-left++ …
Prefix Decrementright-to-left-- …
typeofright-to-lefttypeof …
voidright-to-leftvoid …
deleteright-to-leftdelete …
14Multiplicationleft-to-right… * …
Divisionleft-to-right… / …
Remainderleft-to-right… % …
13Additionleft-to-right… + …
Subtractionleft-to-right… - …
12Bitwise Left Shiftleft-to-right… << …
Bitwise Right Shiftleft-to-right… >> …
Bitwise Unsigned Right Shiftleft-to-right… >>> …
11Less Thanleft-to-right… < …
Less Than Or Equalleft-to-right… <= …
Greater Thanleft-to-right… > …
Greater Than Or Equalleft-to-right… >= …
inleft-to-right… in …
instanceofleft-to-right… instanceof …
10Equalityleft-to-right… == …
Inequalityleft-to-right… != …
Strict Equalityleft-to-right… === …
Strict Inequalityleft-to-right… !== …
9Bitwise ANDleft-to-right… & …
8Bitwise XORleft-to-right… ^ …
7Bitwise ORleft-to-right… | …
6Logical ANDleft-to-right… && …
5Logical ORleft-to-right… || …
4Conditionalright-to-left… ? … : …
3Assignmentright-to-left… = …
… += …
… -= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2yieldright-to-leftyield …
1Spreadn/a... …
0Comma / Sequenceleft-to-right… , …