查看: 3779|回复: 0

[HTML/CSS/JS] 4种Javascript类型检测的方式

[复制链接]

等级头衔

积分成就    金币 : 2865
   泡泡 : 1516
   精华 : 6 篇
   在线时间 : 1339 小时
   最后登录 : 2026-12-3

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2022-4-12 08:05:25 | 显示全部楼层 |阅读模式
一、typeof% ]0 l& x: O8 i( U7 J$ q
       主要用于判断基本数据类型 。使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算。typeof运算符的返回类型为字符串,值包括如下几种:
; m4 w/ G" M" W6 M! N8 w  q: S
  • 'undefined':未定义的变量或值
  • 'boolean':布尔类型的变量或值
  • 'string' :字符串类型的变量或值
  • 'number':数字类型的变量或值
  • 'object' :对象类型的变量或值,或者null(这个是js历史遗留问题,将null作为object类型处理)
  • 'function' :函数类型的变量或值7 X7 m6 s1 s% z: @
示例如下:
, ~$ g% t9 `/ k2 b6 k
  1. console.log(typeof a);    //'undefined'   
  2. console.log(typeof(true));  //'boolean'   
  3. console.log(typeof '123');  //'string'   
  4. console.log(typeof 123);   //'number'   
  5. console.log(typeof NaN);   //'number'   
  6. console.log(typeof null);  //'object'      
  7. var obj = new String();    console.log(typeof(obj));    //'object'   
  8. var  fn = function(){};    console.log(typeof(fn));  //'function'   
  9. console.log(typeof(class c{}));  //'function'
typeof的不足之处:! K0 I8 r# E# d5 I5 l
  • 不能区分对象、数组、正则,对它们操作都返回"object";(正则特殊一点后面说)
  • Safar5,Chrome7之前的版本对正则对象返回 'function'
  • 在IE6,7和8中,大多数的宿主对象是对象,而不是函数;如:typeof alert; //object
  • 而在非ID浏览器或则IE9以上(包含IE9),typeof alert; //function
  • 对于null,返回的是object.. U2 _. H) P: x* ~
总结:
% o( N5 w1 @+ T# c       typeof运算符用于判断对象的类型,但是对于一些创建的对象,它们都会返回'object',有时我们需要判断该实例是否为某个对象的实例,那么这个时候需要用到instanceof运算符。
3 y- S' v# q* h6 `; O" Y0 R7 r二、instanceof5 s; S9 H; f( G8 H  J, N2 g
       用于引用数据类型的判断。所有引用数据类型的值都是Object的实例。目的是判断一个对象在其原型链上是否存在构造函数的prototype属性。用法:
# z2 l* W3 M2 ~6 |
  1. variable instanceof constructor
示例如下:
' `" w& F* o0 k  e0 o  K3 G, [) i
  1. // example
  2. var arr = [];
  3. 由于:
  4. 1. arr.constructor === Array
  5. 2. arr.__proto__ === Array.prototype
  6. 3. arr.__poto__.proto__ === Object.prototype
  7. 所以, 以下都返回true
  8. 1. arr instanceof arr.constructor(Array)
  9. 2. arr instanceof arr.__proto__.constructor(Array)
  10. 3. arr instanceof arr.__proto__.__poto__.constructor(Object)
  11. 如果你了解原型链的话,你很快就会得出一些结论:
  12. 1. 所有对象 instanceof Object 都会返回 true
  13. 2. 所有函数 instanceof Function 都会返回 true
总结:1 j& S6 g$ i( G$ }
       instanceof不仅能检测构造对象的构造器,还检测原型链。instanceof要求前面是个对象,后面是一个构造函数。而且返回的是布尔型的,不是true就是false。& l7 ]& i7 R+ ~4 b& }
三、Array.isArray()5 [3 B2 h$ V6 }" w$ @
       Array.isArray()可以用于判断数组类型,支持的浏览器有IE9+、FireFox 4+、Safari 5+、Chrome; 兼容实现:
/ |6 T; L$ ]) k9 ?" D& P
  1. if (!Array.isArray) {
  2.   Array.isArray = function(arg) {
  3.     return Object.prototype.toString.call(arg) === '[object Array]';
  4.   };
  5. }
示例如下:
  v5 u( j6 M) [
  1. // 1.
  2. Array.isArray([1, 2, 3, 4]);  // --> true
  3. // 2.
  4. var obj = {
  5.     a: 1,
  6.     b: 2
  7. };
  8. Array.isArray(obj);  // --> false
  9. // 3.
  10. Array.isArray(new Array);  // --> true
  11. //4.
  12. Array.isArray("Array");  // --> false
总结:* J' Y% t0 F2 g8 v2 `1 ?. n
       isArray是一个静态方法,使用Array对象(类)调用,而不是数组对象实例。其中Array.prototype 也是一个数组,Array.isArray 优于 instanceof。3 m7 U; L, Y$ L7 J( U0 ?
四、Object.prototype.toString.call()* j1 S5 w6 v1 l9 n4 l2 `/ i' b0 g0 n0 x
       判断某个对象值属于哪种内置类型, 最靠谱的做法就是通过Object.prototype.toString方法。object.prototype.toString()输出的格式就是[object 对象数据类型]。, n6 o' S; Q' L' @
示例如下:- m) y" j. R; b4 F, |
  1. console.log(Object.prototype.toString.call("jerry"));//[object String]
  2. console.log(Object.prototype.toString.call(12));//[object Number]
  3. console.log(Object.prototype.toString.call(true));//[object Boolean]
  4. console.log(Object.prototype.toString.call(undefined));//[object Undefined]
  5. console.log(Object.prototype.toString.call(null));//[object Null]
  6. console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
  7. console.log(Object.prototype.toString.call(function(){}));//[object Function]
  8. console.log(Object.prototype.toString.call([]));//[object Array]
  9. console.log(Object.prototype.toString.call(new Date));//[object Date]
  10. console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
  11. function Person(){};
  12. console.log(Object.prototype.toString.call(new Person));//[object Object]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|paopaomj.COM

相关侵权、举报、投诉及建议等,请发 E-mail:admin@paopaomj.com

Powered by PPMJ X5.0 |渝ICP备18007172号|渝公网安备50010502503914号

在本版发帖QQ客服返回顶部