js數據類型檢測四種方法

一、typeof 用來檢測數據類型的運算符返回的都是字符串其次字符串中包含了對應的數據類型

number
string
boolenan
undefined
function
Object
typeof typeof typeof function(){}->返回結果string

侷限性javascript

二、instaceof 檢測某一個實例是否屬於一個類

侷限性java

(1 instaceof Number) //false
(true instaceof Boolean) //false
(new Number(1) instaceof Number) //true

一、字面量建立出來的結果和實例建立出來的結果是有必定區別的只有實例建立出來的對象纔是標準的對象函數

二、實例prototype

var arr = [];
(arr instaceof Array)//true
(arr instaceof Object)//true
(fn instaceof Function) //true
(fn instaceof Object) // true

三、constructor 構造函數

var arr = [];
    console.log(arr.constructor===Array);

四、Object.prototype.toString.call();

console.log(typeof 12); 

function dd (callbak){

    callbak && callbak();

}

var obj = [12,32];

console.log(obj instanceof Array);
console.log(Object.prototype.toString.call([]));
 console.log(Object.prototype.toString.call({}));
console.log(Object.prototype.toString.call(1));
console.log(Object.prototype.toString.call("sff"));
console.log(Object.prototype.toString.call(new Date));
console.log(Object.prototype.toString.call(/\d/));
console.log(Object.prototype.toString.call(null));
console.log(Object.prototype.toString.call(undefined));
相關文章
相關標籤/搜索