电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

JavaScript 判斷數據類型的4種方法

瀏覽:93日期:2023-10-14 10:30:56

本文提供四種方法判斷js數據類型,這里記錄了它們之間的差異,分別是 typeof 運算符、instanceof 運算符、constructor 屬性、Object.prototype.toString 方法。

一、使用 typeof 判斷數據類型

console.log(’測試 Number ->’, typeof 1); // numberconsole.log(’測試 Boolean ->’, typeof true); // booleanconsole.log(’測試 String ->’, typeof ’’); // stringconsole.log(’測試 null ->’, typeof null); // objectconsole.log(’測試 undefined ->’, typeof undefined); // undefinedconsole.log(’測試 NaN ->’, typeof NaN); // numberconsole.log(’測試 function ->’, typeof function () { }); // functionconsole.log(’測試 Object ->’, typeof {}); // objectconsole.log(’測試 Array ->’, typeof []); // objectconsole.log(’測試 Date ->’, typeof new Date()); // objectconsole.log(’測試 Error ->’, typeof new Error()); // objectconsole.log(’測試 RegExp ->’, typeof new RegExp()); // objectconsole.log(’測試 Symbol ->’, typeof Symbol()); // symbolconsole.log(’測試 Map ->’, typeof new Map()); // objectconsole.log(’測試 Set ->’, typeof new Set()); // object

控制臺輸出如下:

測試 Number -> number測試 Boolean -> boolean測試 String -> string測試 null -> object測試 undefined -> undefined測試 NaN -> number測試 function -> function測試 Object -> object測試 Array -> object測試 Date -> object測試 Error -> object測試 RegExp -> object測試 Symbol -> symbol測試 Map -> object測試 Set -> object

總結:

1、typeof只能判斷:

String(返回string), Number(返回number), Boolean(返回boolean), undefined(返回undefined), function(返回function), Symbol(返回symbol)

2、對于new構造出來的都是返回object

3、對于Object和Array都是返回object

二、使用 instanceof 判斷數據類型

console.log(’測試 Number ->’, 1 instanceof Number); // falseconsole.log(’測試 Boolean ->’, true instanceof Boolean); // falseconsole.log(’測試 String ->’, ’’ instanceof String); // false// console.log(’測試 null ->’, null instanceof null); // TypeError: Cannot read property ’constructor’ of null// console.log(’測試 undefined ->’, undefined instanceof undefined); // TypeError: Cannot read property ’constructor’ of undefinedconsole.log(’測試 NaN ->’, NaN instanceof Number); // falseconsole.log(’測試 function ->’, function () { } instanceof Function); // trueconsole.log(’測試 Object ->’, {} instanceof Object); // trueconsole.log(’測試 Array ->’, [] instanceof Array); // trueconsole.log(’測試 Date ->’, new Date() instanceof Date); // trueconsole.log(’測試 Error ->’, new Error() instanceof Error); // trueconsole.log(’測試 RegExp ->’, new RegExp() instanceof RegExp); // trueconsole.log(’測試 Symbol ->’, Symbol() instanceof Symbol); // falseconsole.log(’測試 Map ->’, new Map() instanceof Map); // trueconsole.log(’測試 Set ->’, new Set() instanceof Set); // trueconsole.log(’測試 new Number ->’, new Number(1) instanceof Number); // trueconsole.log(’測試 new Boolean ->’, new Boolean(true) instanceof Boolean); // trueconsole.log(’測試 new String ->’, new String(’’) instanceof String); // true

控制臺輸出如下:

測試 Number -> false測試 Boolean -> false測試 String -> false測試 NaN -> false測試 function -> true測試 Object -> true測試 Array -> true測試 Date -> true測試 Error -> true測試 RegExp -> true測試 Symbol -> false測試 Map -> true測試 Set -> true測試 new Number -> true測試 new Boolean -> true測試 new String -> true

總結:

1、不能判斷 null,undefined

2、基本數據類型 Number,String,Boolean 不能被判斷

3、instanceof 用來判斷對象是否為某一數據類型的實例,上例中1,true,’’不是實例,所以判斷為false

三、使用 constructor 判斷數據類型

console.log(’測試 Number ->’, (1).constructor === Number); // trueconsole.log(’測試 Boolean ->’, true.constructor === Boolean); // trueconsole.log(’測試 String ->’, ’’.constructor === String); // true// console.log(’測試 null ->’, null.constructor === null); // TypeError: Cannot read property ’constructor’ of null// console.log(’測試 undefined ->’, undefined.constructor); // TypeError: Cannot read property ’constructor’ of undefinedconsole.log(’測試 NaN ->’, NaN.constructor === Number); // true 注意:NaN和infinity一樣是Number類型的一個特殊值console.log(’測試 function ->’, function () { }.constructor === Function); // trueconsole.log(’測試 Object ->’, {}.constructor === Object); // trueconsole.log(’測試 Array ->’, [].constructor === Array); // trueconsole.log(’測試 Date ->’, new Date().constructor === Date); // trueconsole.log(’測試 Error ->’, new Error().constructor === Error); // trueconsole.log(’測試 RegExp ->’, new RegExp().constructor === RegExp); // trueconsole.log(’測試 Symbol ->’, Symbol().constructor === Symbol); // trueconsole.log(’測試 Map ->’, new Map().constructor === Map); // trueconsole.log(’測試 Set ->’, new Set().constructor === Set); // true

控制臺輸出如下:

測試 Number -> true測試 Boolean -> true測試 String -> true測試 NaN -> true測試 function -> true測試 Object -> true測試 Array -> true測試 Date -> true測試 Error -> true測試 RegExp -> true測試 Symbol -> true測試 Map -> true測試 Set -> true

總結:

不能判斷null,undefined,其它的都可以

四、使用 Object.prototype.toString 判斷數據類型

console.log(’測試 Number ->’, Object.prototype.toString.call(1)); // [object Number]console.log(’測試 Boolean ->’, Object.prototype.toString.call(true)); // [object Boolean]console.log(’測試 String ->’, Object.prototype.toString.call(’’)); // [object String]console.log(’測試 null ->’, Object.prototype.toString.call(null)); // [object Null]console.log(’測試 undefined ->’, Object.prototype.toString.call(undefined)); // [object Undefined]console.log(’測試 NaN ->’, Object.prototype.toString.call(NaN)); // [object Number]console.log(’測試 function ->’, Object.prototype.toString.call(function () { })); // [object Function]console.log(’測試 Object ->’, Object.prototype.toString.call({})); // [object Object]console.log(’測試 Array ->’, Object.prototype.toString.call([])); // [object Array]console.log(’測試 Date ->’, Object.prototype.toString.call(new Date())); // [object Date]console.log(’測試 Error ->’, Object.prototype.toString.call(new Error())); // [object Error]console.log(’測試 RegExp ->’, Object.prototype.toString.call(new RegExp())); // [object RegExp]console.log(’測試 Symbol ->’, Object.prototype.toString.call(Symbol())); // [object Symbol]console.log(’測試 Map ->’, Object.prototype.toString.call(new Map())); // [object Map]console.log(’測試 Set ->’, Object.prototype.toString.call(new Set())); // [object Set]

控制臺輸出如下:

測試 Number -> [object Number]測試 Boolean -> [object Boolean]測試 String -> [object String]測試 null -> [object Null]測試 undefined -> [object Undefined]測試 NaN -> [object Number]測試 function -> [object Function]測試 Object -> [object Object]測試 Array -> [object Array]測試 Date -> [object Date]測試 Error -> [object Error]測試 RegExp -> [object RegExp]測試 Symbol -> [object Symbol]測試 Map -> [object Map]測試 Set -> [object Set]

總結:

目前最完美的判斷數據類型的方法

結語:以上為筆者的測試和總結。如有誤或不完整地方,歡迎各位老鐵指正。

以上就是JavaScript 判斷數據類型的4種方法的詳細內容,更多關于JavaScript判斷數據類型的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 加盟店-品牌招商加盟-创业项目商机平台 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 软启动器-上海能曼电气有限公司 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 江苏远邦专注皮带秤,高精度皮带秤,电子皮带秤研发生产 | 酒万铺-酒水招商-酒水代理| 山东集装箱活动房|济南集装箱活动房-济南利森集装箱有限公司 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 泰安办公家具-泰安派格办公用品有限公司| 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 山东氧化铁红,山东铁红-淄博科瑞化工有限公司 | 屏蔽服(500kv-超高压-特高压-电磁)-徐吉电气 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 计算机毕业设计源码网| 山东集装箱活动房|济南集装箱活动房-济南利森集装箱有限公司 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | GEDORE扭力螺丝刀-GORDON防静电刷-CHEMTRONICS吸锡线-上海卓君电子有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | 铝合金电阻-无源谐波滤波器-上海稳达电讯设备厂 | 电力电子产业网 | 洗砂机械-球磨制砂机-洗沙制砂机械设备_青州冠诚重工机械有限公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 健身器材-健身器材厂家专卖-上海七诚健身器材有限公司 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | Brotu | 关注AI,Web3.0,VR/AR,GPT,元宇宙区块链数字产业 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 余姚生活网_余姚论坛_余姚市综合门户网站 | 仪器仪表网 - 永久免费的b2b电子商务平台 | 二手Sciex液质联用仪-岛津气质联用仪-二手安捷伦气质联用仪-上海隐智科学仪器有限公司 |