判斷js中的資料型別的幾種方法

weixin_34162695發表於2019-01-10

1、最常見的判斷方法:typeof

alert(typeof a)   ------------> string
alert(typeof b)   ------------> number
alert(typeof c)   ------------> object
alert(typeof d)   ------------> object
alert(typeof e)   ------------> function
alert(typeof f)   ------------> function
// 其中typeof返回的型別都是字串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
// 另外typeof 可以判斷function的型別;在判斷除Object型別的物件時比較方便。

複製程式碼

typeof的缺點

1.不能區分物件、陣列、正則,對它們操作都返回"object";(正則特殊一點後面說)

2.Safar5,Chrome7之前的版本對正則物件返回 'function'

3.在IE6,7和8中,大多數的宿主物件是物件,而不是函式;如:typeof alert; //object

4.特殊中的特殊

typeof 1/0; //NaN(這個NaN不是字串型別,是數值型別)
typeof typeof 1/0; //NaN(這個NaN不是字串型別,是數值型別)
typeof(1/0); //"number"
typeof typeof(1/0); //"string"
typeof(typeof 1/0); //"number"
複製程式碼

2.使用instanceof

console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(arr instanceof Array);// true
console.log(nul instanceof Object);// false
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true

var bool2 = new Boolean()
console.log(bool2 instanceof Boolean);// true

var num2 = new Number()
console.log(num2 instanceof Number);// true

var str2 = new String()
console.log(str2 instanceof String);//  true

function Person(){}
var per = new Person()
console.log(per instanceof Person);// true

function Student(){}
Student.prototype = new Person()
var haoxl = new Student()
console.log(haoxl instanceof Student);// true
console.log(haoxl instanceof Person);// true
注意:
1.instanceof 後面一定要是物件型別,並且大小寫不能錯,該方法適合一些條件選擇或分支。
2.從結果中看出instanceof不能區別undefined和null,而且對於基本型別如果不是用new宣告的則也測試不出來,
對於是使用new宣告的型別,它還可以檢測出多層繼承關係。

複製程式碼

3、根據物件的constructor判斷: constructor

console.log(bool.constructor === Boolean);// true
console.log(num.constructor === Number);// true
console.log(str.constructor === String);// true
console.log(arr.constructor === Array);// true
console.log(obj.constructor === Object);// true
console.log(fun.constructor === Function);// true

console.log(haoxl.constructor === Student);// false
console.log(haoxl.constructor === Person);// true
注意:null和underfine 沒有constructor,並且使用 constructor不安全,因為 constructor指向可以改變
複製程式碼

4、使用Object.prototype.toString.call

alert(Object.prototype.toString.call(a) === ‘[object String]’) -------> true;
alert(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true;
alert(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true;
alert(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true;
alert(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true;
alert(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;

原理(摘自高階程式設計3):在任何值上呼叫 Object 原生的 toString() 方法,都會返回一個 
[object NativeConstructorName] 格式的字串。每個類在內部都有一個 [[Class]]屬性,這個屬性中就指定了
上述字串中的建構函式名。但是它不能檢測非原生建構函式的建構函式名。
注意:大小寫不能寫錯 基本上通用
複製程式碼

5、使用jq提供的jquery.type()

如果物件是undefined或null,則返回相應的“undefined”或“null”。
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"
如果物件有一個內部的[[Class]]和一個瀏覽器的內建物件的 [[Class]] 相同,
我們返回相應的 [[Class]] 名字。 (有關此技術的更多細節。 )
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
其他一切都將返回它的型別“object”。
注:$.type()內部原理就是用的Object.prototype.toString.call()
複製程式碼

相關文章