1 typeof x === "string"
2 typeof(x) === "string' // 小寫 3 x.constructor === String // 大寫類型
同理:Number, Boolean Function 類型也可以這樣檢測
注意:object不同
var obj = { 'age':19, 'put':function(){ return 0; } }
這樣定義的對象與前面規則一樣
var Obj = function(){ this.age = 12; this.put = function(){ return 0; } } var obj = new Obj; typeof obj = "object"
通過構造函數構建對象,obj.constructor = ƒ Function() { [native code] } typeof obj = "object"
所以Object類型檢測要注意構造方法。