js 與 jquery 判斷資料型別的四種方法

2021-10-23 22:06:10 字數 1491 閱讀 4037

1.typeof

number、string、undefined、bollean、object、function

console.log(typeof 1); //number

console.log(typeof true); //bollean

console.log(typeof '歡喜'); //string

console.log(typeof {}); //object

console.log(typeof ); //object

2.tostring.call()
console.log(tostring.call(666)); //[object number]

console.log(tostring.call(true)); //[object boolean]

console.log(tostring.call('歡喜')); //[object string]

console.log(tostring.call(undefined)); //[object undefined]

console.log(tostring.call({})); //[object object]

console.log(tostring.call()); //[object array]

console.log(tostring.call(function(){})); //[object function]

3.instanceof

a instanceof b 可以判斷a是不是b的例項,返回乙個布林值,由構造型別判斷出資料型別

console.log(arr instanceof array ); // true

console.log(date instanceof date ); // true

console.log(fn instanceof function ); // true

4.根據物件的 contructor 判斷
console.log(arr.constructor === array); //true

console.log(date.constructor === date); //true

console.log(fn.constructor === function); //true

返回乙個布林值

jquery物件.isarray(); //判斷是否為陣列

jquery物件.isemptyobject(); //判斷是否為空物件

jquery物件.isfunction(): //判斷是否為函式

jquery物件.isnumberic(): //判斷是否為數字

jquery物件.iswindow(): //判斷是否為window物件

jquery物件.isxmldoc(): //判斷判斷乙個dom節點是否處於xml文件中

js判斷資料型別

1 typeof 形如 var x xx typeof x string 返回型別有 undefined string number boolean function object 缺點 對於object型別不能細分是什麼型別 優點 對空null的判斷 undefined 的應用 2 instanc...

js判斷資料型別

了解js的都知道,有個typeof 用來判斷各種資料型別,有兩種寫法 typeof typeof 如下例項 typeof 2 輸出 number typeof null 輸出 object typeof 輸出 object typeof 輸出 object typeof function 輸出 fu...

js判斷資料型別

1 判斷是否為陣列型別 2 判斷是否為字串型別 3 判斷是否為數值型別 isnan 變數 如果為true就是數字型別 注意這個函式一般針對數字型別來判斷是否值為nan,若變數為非數字型別,則先轉化為數字型別再做判斷,用此函式時,別忘考慮空串和空格 這倆轉化為數字是0 4 判斷是否為日期型別 5 判斷...