javascript 判斷物件類型

來源:互聯網
上載者:User

標籤:

以下引用 http://bushihentian.blog.163.com/blog/static/187174306201401371757775/

方式一

使用typeof 關鍵字 傳回值有 ‘string‘ ‘number‘ ‘boolean‘  ‘undfined‘ ‘function‘ ‘object‘ 共六鐘
typeof // typeof 可以用來檢測給定變數的資料類型,返回一個字串
console.log(typeof ‘‘); // string
console.log(typeof null); // object
console.log(typeof 1); // number
console.log(typeof {name:‘name‘}); //object
console.log(typeof undefined); //undefined
console.log(typeof [1,2]); //object
console.log(typeof true); //boolean
console.log(typeof function(){}); //function
總結:從上面的例子可以看出,除了 null ,對象,數組以外的其他變數類型都可以準確的檢測出是什麼資料類型。
null,對象,數組都返回了 object

判斷使用typeof 判斷是什麼基本類型,如果是‘object‘,就可以使用toString擷取該對象是什麼類型

方式二

使用 instanseof 關鍵字

instanseof // 用於判斷一個變數是否某個對象的執行個體,返回boolean值

var str = ‘123‘;
console.log(str instanceof String);  // false

var num = 123;
console.log(num instanceof Number); // false

var bool = true;
console.log(bool instanceof Boolean); // false

var arr = [1,2,3];
console.log(arr instanceof Array); // true

var obj = {name:‘name‘};
console.log(obj instanceof Object); // true

 

總結:從上面的例子可以看出,普通定義的變數除了數組和對象會返回true,其他的簡單類型都返回false.

 

方式三

 

使用 constructor 屬性

constructor // 每個變數都有一個constructor屬性,指向自己的建構函式

var str = ‘123‘;
console.log(str.constructor === String); // true

var num = 123;
console.log(num.constructor === Number); // true

var bool = true;
console.log(bool.constructor === Boolean); // true

var arr = [1,2,3];
console.log(arr.constructor === Array); // true

var obj = {name:‘name‘};
console.log(obj.constructor === Object); // true

 

總結:從上面的例子可以看出,每個變數的建構函式都指向了他們的類型.

方式四

所有的對象的prototype最終都指向Object的prototype屬性,所以通過Object的toString方法去擷取傳回值

var str = ‘123‘;
console.log(Object.prototype.toString.call(str)); // "[object String]"

var num = 123;
console.log(Object.prototype.toString.call(num)); // "[object Number]"

var bool = true;
console.log(Object.prototype.toString.call(bool)); // "[object Boolean]"

var arr = [1,2,3];
console.log(Object.prototype.toString.call(arr)); // "[object Array]"

var obj = {name:‘name‘};
console.log(Object.prototype.toString.call(obj)); // "[object Object]"


var obj = null;

Object.prototype.toString.call(str); // "[object Null]"

總結:通過正則或字元截取去獲得大寫開頭的類型字串,然後通過String.toLowerCase()方法擷取對應變數的資料類型。

暫時只發現這幾種方法,我們可以根據自己的需求去使用對應的方式去封裝getType() 這樣的方法

 注意自訂的對象返回 "[object Object]" 如:

function Person(){}
var p=new Person();

(Object.prototype.toString.call(p);// "[object Object]"

javascript 判斷物件類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.