1. 前置知識(shí)
2. -
*
/
%
運(yùn)算符
Number類型直接進(jìn)行數(shù)學(xué)運(yùn)算,非Number類型,轉(zhuǎn)換成Number類型后刻盐,進(jìn)行數(shù)學(xué)運(yùn)算
3. +
運(yùn)算符
3.1 作為一元運(yùn)算符
表示對(duì)對(duì)象進(jìn)行Number()
轉(zhuǎn)換
console.log(+{});//NaN
console.log(+["Lawson", "Nion"]);//NaN
console.log(+[]);//0
console.log(+false);//0
console.log(+"123");//123
console.log(+null);//0
console.log(+undefined);//NaN
3.2 作為二目運(yùn)算符
+
號(hào)左右兩邊有一個(gè)有優(yōu)先級(jí)高的噪珊,就往這個(gè)類型轉(zhuǎn)
- 優(yōu)先級(jí)順序:
String
>Number
>Boolean
==null
==undefined
-
Object
特殊處理降传,得到基本類型后再進(jìn)行優(yōu)先級(jí)判定- 若有
valueOf()
方法舶治,則先調(diào)用琼讽,若得到基本類型熔萧,則使用 - 若沒有
valueOf()
方法或者valueOf()
方法返回不是基本類型糖驴,則嘗試調(diào)用toString()
方法 - 若有
toString()
方法,則調(diào)用佛致,若得到基本類型贮缕,則使用 - 若沒有
toString
方法,或者toString
得到的不是基本類型俺榆,則報(bào)錯(cuò)
- 若有
- 從左至右進(jìn)行分割運(yùn)算
- 優(yōu)先級(jí)低的向優(yōu)先級(jí)高的類型進(jìn)行轉(zhuǎn)換
- 如果轉(zhuǎn)換后兩邊都不是String或者Number感昼,則往Number轉(zhuǎn)
let obj = {
name: "Lawson",
toString() {
console.log("called toString");
return false;
},
valueOf() {
console.log("called valueOf");
return [];
},
};
console.log(1 === 1 + obj);
//called valueOf
//called toString
//true
//先得到了false,Boolean和Number罐脊,Number優(yōu)先級(jí)高抑诸,往Number轉(zhuǎn)換
//1 + false => 1 + 0 => 1
let obj = {
name: "Lawson",
toString() {
console.log("called toString");
return 1;
},
valueOf() {
console.log("called valueOf");
return [];
},
};
console.log(true + obj);
//called valueOf
//called toString
//2
//先得到了1烂琴,Boolean和Number,Number優(yōu)先級(jí)高蜕乡,往Number轉(zhuǎn)換
//true + 1 => 1 + 1 => 2
console.log(true + false);//1
console.log(true + null);//1
console.log(null + undefined);//NaN
console.log(true + undefined);//NaN
//[]調(diào)用valuteOf會(huì)得到""奸绷,是String,所以都往String轉(zhuǎn)
console.log(null + []);//'null'
//{}被識(shí)別為對(duì)象层玲,并通過toString()轉(zhuǎn)換成'[object Object]'
console.log(1 + {});//"1[object Object]"
//{}會(huì)被識(shí)別為代碼塊
console.log({} + 1);//1
//1 + false => 1 + 0 => 1
//1 + '2' => '1' + '2' => '12'
//'12' + null => '12' + 'null' => '12null'
console.log(1 + false + '2' + null);//'12null'
//1 + false => 1 + 0 => 1
//1+ null => 1 + 0 => 1
//1 + '2' => '1' + '2' => '12'
console.log(1 + false + null + '2');//'12'
//false + null => 0 + 0 => 0
//0 + 1 => 1
//1 + '2' => '1' + '2' => '12'
console.log(false + null + 1 + '2');//'12'
//false + null => 0 + 0 => 0
//0 + '2' => '0' + '2' => '02'
//'02' + 1 => '02' + '1' => '021'
console.log(false + null + '2' + 1);//'021'
4. >
>=
<
<=
邏輯運(yùn)算
Number類型直接進(jìn)行邏輯運(yùn)算号醉,非Number類型,轉(zhuǎn)換成Number類型后辛块,進(jìn)行邏輯運(yùn)算
5. ==
邏輯運(yùn)算
5.1 類型相同畔派,直接比較
-
NaN
和NaN
不等
NaN == NaN //false
-
Object
類型比較堆地址
{} == {} //false
[] == [] //false
[] == {} //false 不進(jìn)行類型轉(zhuǎn)換
//!Boolean([]) == !Boolean({}) => !true == !true => false == false
![] == !{} //true
//[] == !Boolean([]) => [] == !true => Number([]) == Number(false) => 0 == 0
[] == ![] //true
var a = b = {};
a == b //true
-
+0
、-0
润绵、0
相等 - 其他都相等
5.2 類型不同线椰,先轉(zhuǎn)換
-
null
和undefined
相等
null == undefined //true
-
Object
(Date
類型轉(zhuǎn)為String
類型)、Number
尘盼、String
憨愉、Boolean
之間的比較都是返回轉(zhuǎn)換為Number
類型進(jìn)行比較
//Number(123) == Number('123')
123 == '123' //true
//Number(0) == Number(false)
0 == false //true
//Number(0) == Number([]) => 0 == Number('') => 0 == 0
0 == [] //true
//Number(0) == Number({}) => 0 == Number('[object Object]') => 0 == NaN
0 == {} //false
//Number([]) == Number({}) => Number('') == Number('[object Object]') => 0 == NaN
new Date('2020/11/28').valueOf() //1606492800000
new Date('2020/11/28').toString() //"Sat Nov 28 2020 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)"
new Date('2020/11/28') == "Sat Nov 28 2020 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)" //true
new Date('2020/11/28') == 1606492800000 //false
- 其他類型進(jìn)行轉(zhuǎn)換時(shí)都不相等(null和undefined與其他類型都不相等)
null == 0 //false
undefined == {} //false
6. &&
||
邏輯運(yùn)算
6.1 &&
- 從左往右執(zhí)行代碼塊
- 執(zhí)行代碼塊A,得出結(jié)果resA
- 若
Boolean(resA)
為false卿捎,則返回resA配紫,不執(zhí)行代碼塊B - 若
Boolean(resA)
為true,則繼續(xù)執(zhí)行代碼塊B午阵,得出結(jié)果resB - 若
Boolean(resB)
為false躺孝,則返回resB,不執(zhí)行代碼塊C - 若
Boolean(resB)
為true底桂,則繼續(xù)執(zhí)行代碼塊C植袍,得出結(jié)果resC - 依次遞進(jìn),直到
Boolean(resN)
為false籽懦,或者執(zhí)行完所有
function A(){
console.log('called A.');
return [] == ![];
}
function B(){
console.log('called B.');
return undefined + 0;
}
function C(){
console.log('called C.');
return true;
}
console.log(A() && B() && C());//NaN
//called A.
//called B.
//NaN
//A執(zhí)行奋单,結(jié)果為[] == ![],通過Boolean([] == ![]) => true猫十,會(huì)執(zhí)行一個(gè)表達(dá)式
//B執(zhí)行览濒,結(jié)果為undefined + 0,通過Boolean(undefined + 0) => false拖云,會(huì)停止向下執(zhí)行贷笛,返回undefined + 0的表達(dá)式結(jié)果undefined + 0 => Number(undefined) + 0 => NaN + 0 => NaN
//C不會(huì)執(zhí)行
6.2 ||
- 每一步的返回值進(jìn)行Boolean操作
- 返回第一個(gè)可轉(zhuǎn)換為true的表達(dá)式
- 可轉(zhuǎn)換為true的表達(dá)式后面的表達(dá)式會(huì)被【短路】(不會(huì)執(zhí)行)
function A() {
console.log("called A.");
return [] == [];
}
function B() {
console.log("called B.");
return null + 1;
}
function C() {
console.log("called C.");
return true;
}
console.log(A() || B() || C());
//called A.
//called B.
//1
//A執(zhí)行,return結(jié)果為[] == []宙项,類型相同乏苦,直接比較堆地址,不等,計(jì)算結(jié)果為false汇荐,繼續(xù)向右執(zhí)行
//B執(zhí)行洞就,結(jié)果為null + 1,通過Boolean(null + 1) => true掀淘,會(huì)停止向下執(zhí)行旬蟋,返回null + 1的表達(dá)式結(jié)果null + 1 => Number(null) + 1 => 0 + 1 => 1
//C不會(huì)執(zhí)行