一、隱式轉換 (+ 和 -,== 和 ===)
1. +
和 -
console.log("16" + 6) // "166"
console.log("16" - 6) // 10
2. 比較運算符
==
(等于),會自動轉換數據類型再比較===
(嚴格等于),不會自動轉換數據類型,如果數據類型不一致,返回false;如果一致,再比較。
false == 0; // true
false === 0; // false
undefined == null // true,(undefined是null的子集)
由于JavaScript這個設計缺陷,不要使用 ==
比較,始終堅持使用 ===
比較。
- NaN(Not a Number)這個特殊的Number與所有其他值都不相等,包括它自己:
NaN === NaN; // false
唯一能判斷 NaN
的方法是通過 isNaN()
函數:
isNaN(NaN); // true
- 浮點數相等比較
1 / 3 === (1 - 2 / 3); // false
浮點數在運算過程中會產生誤差,因為計算機無法精確表示無限循環小數。要比較兩個浮點數是否相等,只能計算它們之差的絕對值,看是否小于某個閾值:
Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true
二、包裝對象
在JavaScript中,一切皆對象。
Array
(數組)和 Function
(函數)本質上都是對象,就連三種原始類型的值 — — Number
(數值)、String
(字符串)、Boolean
(布爾值) — — 在一定條件下,也會自動轉為對象,也就是原始類型的 包裝對象。
通俗來講:Number
,String
,Boolean
,這些單身狗看其它人都有對象,非常不爽,所以自己也搞了個對象,名字叫包裝。
- 一般來說,只有對象是可以對屬性進行讀寫操作的。
但是,你有沒有發現,平時我們用得很多的字符串方法和屬性,都是直接通過
.
操作符訪問的。比如:
console.log("hello world".length);
console.log("this a string".indexOf("a"));
其實,在我們調用這些方法和屬性時,JS內部已經隱式地幫我們幫創建了一個包裝對象了,以上的實際的情形應該是這樣的:
console.log(new String("hello world").length);
console.log(new String("this a string").indexOf("a"));
瀏覽器自己隱式創建的包裝對象和你顯式創建的包裝對象不嚴格相等。簡單來說,雖然說表面JS對親生的與領養的一樣,但實際上,親生的不等于領養的。
var a1 = "test";
var a2 = new String("test");
console.log(a1 == a2); // true
console.log(a1 === a2); // false
三、類型檢測
在js中檢測類型的方法有很多,比如 typeof
、instanceof
、constructor
和 Object.prototype.toString
。
1. typeof運算符
typeof
可以檢測變量的數據類型,返回如下6種字符串:number
、string
、boolean
、object
、undefined
、function
var x = 1;
console.log(typeof x); // number
var a = undefined;
console.log(typeof a); // undefined
var b = null;
console.log(typeof b); // object,(null是空對象引用/或者說指針)。
var c = new Object();
console.log(typeof c); // object
var e = [1,2,3];
console.log(typeof e); // object
var d = function(){}
console.log(typeof d); // function
typeof
在檢測對象類型(array
、object
)的時候是無法區分的,此時我們就要用第二種方法 instanceof
2. instanceof運算符
instanceof
,用于檢測某個對象的原型鏈是否包含某個構造函數的prototype
屬性。
instanceof
適用于檢測對象,它是基于原型鏈運作的。
-
instanceof
除了適用于任何object
的類型檢查之外,也可以用來檢測內置對象,比如:Array
、RegExp
、Object
、Function
:
[1, 2, 3] instanceof Array // true
/abc/ instanceof RegExp // true
({}) instanceof Object // true
(function(){}) instanceof Function // true
-
instanceof
對基本數據類型檢測不起作用,主要是因為基本數據類型沒有原型鏈。
3. constructor屬性
構造函數屬性,可確定當前對象的構造函數。
var o = new Object();
console.log(o.constructor == Object); // true
var arr = new Array();
console.log(arr.constructor == Array); // true
4. Object.prototype.toString
Object.prototype.toString.call()
或者Object.prototype.toString.apply()
5.hasOwnProperty(propertyName)
判斷屬性是否存在于當前對象實例中(而不是原型對象中)。
總結
一、 隱式類型轉換的場景
二、顯式類型轉換
Number()
、String()
、Boolean()
、parseInt()
、parseFloat()
、!
、!!
三、類型識別
typeof
、instanceof
、Object.prototype.toString.call
、constructor
- typeof操作符
-
typeof
可以識別除了null
以外的標準類型 -
typeof
不能識別具體的對象類型 (Function除外)
-
- instanceof
- 判別內置對象類型
- 不能判別原始類型
- 判別自定義對象類型
- Object.prototype.toString.call
function type(obj) {
return Object.prototype.toString.call(obj).slice(8,-1);
}
- constructor
構造這個對象的構造函數本身
封裝方法
function getConstructorName (obj) {
return (obj===undefined||obj===null) ? obj : (obj.constructor&&obj.constructor.toString().match(/function\s*([^(]*)/)[1]);
}