成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

四種Javascript類(lèi)型檢測(cè)的方式

開(kāi)發(fā) 前端
今天這篇文章,主要介紹了JavaScript中檢測(cè)數(shù)據(jù)類(lèi)型的4種方式。如果已經(jīng)會(huì)了的可以當(dāng)成復(fù)習(xí),如果還不了解的話(huà),可以認(rèn)真看看,并加以運(yùn)用。

一、typeof

主要用于判斷基本數(shù)據(jù)類(lèi)型 。使用方式:typeof(表達(dá)式)和typeof 變量名,第一種是對(duì)表達(dá)式做運(yùn)算,第二種是對(duì)變量做運(yùn)算。 typeof運(yùn)算符的返回類(lèi)型為字符串,值包括如下幾種:

  1. 'undefined':未定義的變量或值
  2. 'boolean':布爾類(lèi)型的變量或值
  3. 'string' :字符串類(lèi)型的變量或值
  4. 'number':數(shù)字類(lèi)型的變量或值
  5. 'object' :對(duì)象類(lèi)型的變量或值,或者null(這個(gè)是js歷史遺留問(wèn)題,將null作為object類(lèi)型處理)
  6. 'function' :函數(shù)類(lèi)型的變量或值

示例如下:

console.log(typeof a);    //'undefined'    
console.log(typeof(true)); //'boolean'
console.log(typeof '123'); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof null); //'object'
var obj = new String(); console.log(typeof(obj)); //'object'
var fn = function(){}; console.log(typeof(fn)); //'function'
console.log(typeof(class c{})); //'function'

typeof的不足之處:

  1. 不能區(qū)分對(duì)象、數(shù)組、正則,對(duì)它們操作都返回"object";(正則特殊一點(diǎn)后面說(shuō))
  2. Safar5,Chrome7之前的版本對(duì)正則對(duì)象返回 'function'
  3. 在IE6,7和8中,大多數(shù)的宿主對(duì)象是對(duì)象,而不是函數(shù);如:typeof alert; //object
  4. 而在非ID瀏覽器或則IE9以上(包含IE9),typeof alert; //function
  5. 對(duì)于null,返回的是object.

總結(jié):

typeof運(yùn)算符用于判斷對(duì)象的類(lèi)型,但是對(duì)于一些創(chuàng)建的對(duì)象,它們都會(huì)返回'object',有時(shí)我們需要判斷該實(shí)例是否為某個(gè)對(duì)象的實(shí)例,那么這個(gè)時(shí)候需要用到instanceof運(yùn)算符。

二、instanceof

用于引用數(shù)據(jù)類(lèi)型的判斷。所有引用數(shù)據(jù)類(lèi)型的值都是Object的實(shí)例。目的是判斷一個(gè)對(duì)象在其原型鏈上是否存在構(gòu)造函數(shù)的prototype屬性。 用法:

variable instanceof constructor

示例如下:


// example
var arr = [];

由于:
1. arr.constructor === Array
2. arr.__proto__ === Array.prototype
3. arr.__poto__.proto__ === Object.prototype

所以, 以下都返回true
1. arr instanceof arr.constructor(Array)
2. arr instanceof arr.__proto__.constructor(Array)
3. arr instanceof arr.__proto__.__poto__.constructor(Object)


如果你了解原型鏈的話(huà),你很快就會(huì)得出一些結(jié)論:
1. 所有對(duì)象 instanceof Object 都會(huì)返回 true
2. 所有函數(shù) instanceof Function 都會(huì)返回 true

總結(jié):

instanceof不僅能檢測(cè)構(gòu)造對(duì)象的構(gòu)造器,還檢測(cè)原型鏈。instanceof要求前面是個(gè)對(duì)象,后面是一個(gè)構(gòu)造函數(shù)。而且返回的是布爾型的,不是true就是false。

3、Array.isArray()

Array.isArray()可以用于判斷數(shù)組類(lèi)型,支持的瀏覽器有IE9+、FireFox 4+、Safari 5+、Chrome; 兼容實(shí)現(xiàn):

if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}

示例如下:

// 1.
Array.isArray([1, 2, 3, 4]); // --> true

// 2.
var obj = {
a: 1,
b: 2
};
Array.isArray(obj); // --> false

// 3.
Array.isArray(new Array); // --> true

//4.
Array.isArray("Array"); // --> false

總結(jié):

isArray是一個(gè)靜態(tài)方法,使用Array對(duì)象(類(lèi))調(diào)用,而不是數(shù)組對(duì)象實(shí)例。其中Array.prototype 也是一個(gè)數(shù)組,Array.isArray 優(yōu)于 instanceof。

四、Object.prototype.toString.call()

判斷某個(gè)對(duì)象值屬于哪種內(nèi)置類(lèi)型, 最靠譜的做法就是通過(guò)Object.prototype.toString方法。object.prototype.toString()輸出的格式就是[object 對(duì)象數(shù)據(jù)類(lèi)型]。

示例如下:

console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]
責(zé)任編輯:華軒 來(lái)源: 前端Q
相關(guān)推薦

2020-06-12 08:28:29

JavaScript開(kāi)發(fā)技術(shù)

2023-05-22 08:03:28

JavaScrip枚舉定義

2021-07-14 10:31:15

JavaScript開(kāi)發(fā) 技巧

2021-10-24 08:37:18

網(wǎng)絡(luò)監(jiān)控網(wǎng)絡(luò)架構(gòu)網(wǎng)絡(luò)

2013-06-14 15:24:57

Android開(kāi)發(fā)移動(dòng)開(kāi)發(fā)數(shù)據(jù)存儲(chǔ)方式

2010-07-28 13:54:42

Flex數(shù)據(jù)綁定

2017-04-17 19:31:03

Android多線(xiàn)程

2013-10-17 09:25:52

2021-12-01 23:05:27

物聯(lián)網(wǎng)計(jì)算數(shù)據(jù)

2021-12-22 09:34:01

Golagn配置方式

2014-12-25 09:41:15

Android加載方式

2020-05-19 20:13:04

物聯(lián)網(wǎng)計(jì)算類(lèi)型IOT

2021-06-25 08:00:00

物聯(lián)網(wǎng)醫(yī)療技術(shù)

2011-05-20 09:55:26

Oracle連接

2025-01-20 15:50:19

2024-03-20 15:33:12

2022-10-27 14:18:13

Flowable流程變量

2015-09-06 09:23:23

Android異步更新

2025-01-21 09:10:00

2015-04-02 16:54:52

災(zāi)難恢復(fù)VDI災(zāi)難恢復(fù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 一区二区三区免费 | 一二三四在线视频观看社区 | 精品久久久一区 | 国产一区二区在线播放 | 欧美啪啪 | 国产精品不卡一区 | 在线视频国产一区 | 天天操网| 国产99视频精品免视看9 | 日韩综合网 | 九九在线精品视频 | 中文字字幕一区二区三区四区五区 | 中国一级特黄视频 | 日韩免费电影 | 久久精品 | 日韩精品免费 | 中文字幕亚洲在线 | 天天爽天天干 | 伊人春色在线 | 特黄视频 | 欧美日韩精品在线免费观看 | 国产精品成人一区二区三区夜夜夜 | 91资源在线| 国产不卡一区 | 成人午夜免费在线视频 | 精品一区二区三区四区外站 | 热久久久 | 九九99靖品 | 久久久做| 久热伊人 | 亚洲欧美综合精品久久成人 | 涩色视频在线观看 | 国产精品日韩欧美一区二区三区 | 九九久久精品 | 91精品国产综合久久久动漫日韩 | 伊人性伊人情综合网 | 亚洲免费在线观看视频 | 2021狠狠干 | aa级毛片毛片免费观看久 | 日韩精品成人一区二区三区视频 | 欧美激情a∨在线视频播放 成人免费共享视频 |