js數據類型和判斷數據類型的方法

基本數據類型:String、Number、Boolean、Symbol、undefined、Null
引用類型:Object Array Functionjquery

判斷數據類型的方法:瀏覽器

判斷js中的數據類型有一下幾種方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下來主要比較一下這幾種方法的異同。this

先舉幾個例子:prototype

var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
var f = function(){this.name="22";};

一、最多見的判斷方法: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類型的對象時比較方便。

二、判斷已知對象類型的方法:instanceofcode

instanceof原理:
instanceof 檢測一個對象A是否是另外一個對象B的實例的原理是:查看對象B的prototype指向的對象是否在對象A的[[prototype]]鏈上。若是在,則返回true,若是不在則返回false。不過有一個特殊的狀況,當對象B的prototype爲null將會報錯(相似於空指針異常)。regexp

alert(c instanceof Array) ---------------> true
alert(d instanceof Date)
alert(f instanceof Function) ------------> true
alert(f instanceof function) ------------> false

三、根據對象的constructor判斷:constructor對象

alert(c.constructor === Array) ----------> true
alert(d.constructor === Date) -----------> true
alert(e.constructor === Function) -------> true
注意: constructor 在類繼承時會出錯
eg:
      function A(){};
      function B(){};
      A.prototype = new B(); //A繼承自B
      var aObj = new A();
      alert(aobj.constructor === B) -----------> true;
      alert(aobj.constructor === A) -----------> false;
      言歸正傳,解決construtor的問題一般是讓對象的constructor手動指向本身:
      aobj.constructor = A; //將本身的類賦值給對象的constructor屬性
      alert(aobj.constructor === A) -----------> true;
      alert(aobj.constructor === B) -----------> false; //基類不會報true了;

四、通用但很繁瑣的方法:prototype繼承

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;
大小寫不能寫錯,比較麻煩,但勝在通用。

五、無敵萬能方法 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」。

一般狀況下用typeof 判斷就能夠了,遇到預知Object類型的狀況能夠選用instanceof或constructor方法,實在沒轍就使用$.type()方法。

相關文章
相關標籤/搜索