QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

查看: 3050|回复: 0

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

[复制链接]

等级头衔

积分成就    金币 : 2861
   泡泡 : 1516
   精华 : 6
   在线时间 : 1328 小时
   最后登录 : 2026-5-15

丰功伟绩

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

联系方式
发表于 2022-4-12 08:05:25 | 显示全部楼层 |阅读模式
一、typeof
* Q, s: Y7 f$ U4 e0 W" x       主要用于判断基本数据类型 。使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算。typeof运算符的返回类型为字符串,值包括如下几种:
0 Z- f: [( |& ?) C; |+ T! r' ]( _
  • 'undefined':未定义的变量或值
  • 'boolean':布尔类型的变量或值
  • 'string' :字符串类型的变量或值
  • 'number':数字类型的变量或值
  • 'object' :对象类型的变量或值,或者null(这个是js历史遗留问题,将null作为object类型处理)
  • 'function' :函数类型的变量或值
    8 V: _" s- u+ }- Q8 K7 {/ ^
示例如下:& v  R/ d; Z5 c$ H1 {
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的不足之处:
6 Z. n% o6 C3 T: `, t5 w3 S6 P
  • 不能区分对象、数组、正则,对它们操作都返回"object";(正则特殊一点后面说)
  • Safar5,Chrome7之前的版本对正则对象返回 'function'
  • 在IE6,7和8中,大多数的宿主对象是对象,而不是函数;如:typeof alert; //object
  • 而在非ID浏览器或则IE9以上(包含IE9),typeof alert; //function
  • 对于null,返回的是object.) b# f0 W5 j; {
总结:
5 @7 K! a7 X. P1 q  l+ `       typeof运算符用于判断对象的类型,但是对于一些创建的对象,它们都会返回'object',有时我们需要判断该实例是否为某个对象的实例,那么这个时候需要用到instanceof运算符。" C9 @' w$ q% n3 U8 Y- \
二、instanceof% H  L: k" j8 q1 f) L. i
       用于引用数据类型的判断。所有引用数据类型的值都是Object的实例。目的是判断一个对象在其原型链上是否存在构造函数的prototype属性。用法:
9 i1 }/ w# ]/ [( o- J: o6 l5 V
variable instanceof constructor
示例如下:
! X$ }5 S. H, Y1 S
// 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)
 
 
如果你了解原型链的话,你很快就会得出一些结论:
1. 所有对象 instanceof Object 都会返回 true
2. 所有函数 instanceof Function 都会返回 true
总结:& _7 \1 k" f% W! i& E' Z
       instanceof不仅能检测构造对象的构造器,还检测原型链。instanceof要求前面是个对象,后面是一个构造函数。而且返回的是布尔型的,不是true就是false。
8 ~% N- q/ D6 {0 U三、Array.isArray()
: a+ S, t# S1 J, o. |       Array.isArray()可以用于判断数组类型,支持的浏览器有IE9+、FireFox 4+、Safari 5+、Chrome; 兼容实现:
7 i! M2 ]; ~. P, k8 v
if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}
示例如下:5 ^4 m, f6 A# v$ }) B( W# Y- ?, R
// 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
总结:
+ y0 w* `) ]6 X, ~       isArray是一个静态方法,使用Array对象(类)调用,而不是数组对象实例。其中Array.prototype 也是一个数组,Array.isArray 优于 instanceof。$ k9 C$ [7 k( O* O5 N9 E
四、Object.prototype.toString.call()# j" f- R4 E$ L
       判断某个对象值属于哪种内置类型, 最靠谱的做法就是通过Object.prototype.toString方法。object.prototype.toString()输出的格式就是[object 对象数据类型]。0 a  T; y7 t. W- x8 _4 U3 Z$ v
示例如下:
$ Q% O1 O; i; I+ H
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]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2026-5-23 15:24

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表