js中判断对象类型的几种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串。如:"number","string","boolean","object","function","undefined"(可用于判断变量是否存在)。 但 typeof 的能力有限,其对于Date、RegExp类型返回的都是"object"。如:
1
2
3
|
typeof {}; // "object" typeof []; // "object" typeof new Date(); // "object" |
所以它只在区别对象和原始类型的时候才有用。要区一种对象类型和另一种对象类型,必须使用其他的方法。如:instanceof 运算符或对象的 constructor 属。 2)instanceof 运算符。 instanceof 运算符要求其左边的运算数是一个对象,右边的运算数是对象类的名字或构造函数。如果 object 是 class 或构造函数的实例,则 instanceof 运算符返回 true。如果 object 不是指定类或函数的实例,或者 object 为 null,则返回 false。如:
1
2
3
4
|
[] instanceof Array; // true [] instanceof Object; // true [] instanceof RegExp; // false new Date instanceof Date; // true |
所以,可以用instanceof运算符来判断对象是否为数组类型:
1
2
3
|
function isArray(arr){ return arr instanceof Array; } |
3)constructor 属性。 JavaScript中,对象有一个constructor属性,它引用了初始化该对象的构造函数,常用于判断未知对象的类型。如给定一个求知的值 通过typeof运算符来判断它是原始的值还是对象。如果是对象,就可以使用constructor属性来判断其类型。所以判断数组的函数也可以这样写:
1
2
3
|
function isArray(arr){ return typeof arr == "object" && arr.constructor == Array; } |
很多情况下,我们可以使用instanceof运算符或对象的constructor属性来检测对象是否为数组。例如很多JavaScript框架就是使用这两种方法来判断对象是否为数组类型。 但是检测在跨框架(cross-frame)页面中的数组时,会失败。原因就是在不同框架(iframe)中创建的数组不会相互共享其prototype属性。例如:
1
2
3
4
5
6
7
|
< script > window.onload=function(){ var iframe_arr=new window.frames[0].Array; alert(iframe_arr instanceof Array); // false alert(iframe_arr.constructor == Array); // false } </ script > |
在Ajaxian上看到了一种精确的检测方法,跨原型链调用toString()方法:Object.prototype.toString()。可以解决上面的跨框架问题。 当Object.prototype.toString(o)执行后,会执行以下步骤: 1)获取对象o的class属性。 2)连接字符串:"[object "+结果(1)+"]" 3)返回 结果(2) 例如:
1
2
|
Object.prototype.toString.call([]); // 返回 "[object Array]" Object.prototype.toString.call(/reg/ig); // 返回 "[object RegExp]" |
这样,我们就可以写一个健壮的判断对象是否为数组的函数:
1
2
3
|
function isArray(arr){ return Object.prototype.toString.call(arr) === "[object Array]" ; } |
此种方法得到国外多个javaScript大师的认可,在即将发布的jQuery 1.3中将使用这种方法来检测数组。 prototype.js的一个维护者写了下面这个函数,用于获取对象的类型名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * Returns internal [[Class]] property of an object * * Ecma-262, 15.2.4.2 * Object.prototype.toString( ) * * When the toString method is called, the following steps are taken: * 1. Get the [[Class]] property of this object. * 2. Compute a string value by concatenating the three strings "[object ", Result (1), and "]". * 3. Return Result (2). * * __getClass(5); // => "Number" * __getClass({}); // => "Object" * __getClass(/foo/); // => "RegExp" * __getClass(''); // => "String" * __getClass(true); // => "Boolean" * __getClass([]); // => "Array" * __getClass(undefined); // => "Window" * __getClass(Element); // => "Constructor" * */ function __getClass(object){ return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1]; }; |
扩展一下,用于检测各种对象类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var is ={ types : [ "Array" , "Boolean" , "Date" , "Number" , "Object" , "RegExp" , "String" , "Window" , "HTMLDocument" ] }; for ( var i = 0, c; c = is.types[i ++ ]; ){ is[c] = ( function (type){ return function (obj){ return Object.prototype.toString.call(obj) == "[object " + type + "]" ; } )(c); } alert(is.Array([])); // true alert(is.Date( new Date)); // true alert(is.RegExp(/reg/ig)); // true |
js中判断对象类型的几种方法的更多相关文章
- JS中遍历对象属性的四种方法
Object.keys().Object.values().Object.entries().for...in.Map (1)Object.keys(): let ex1 = {c1: 'white' ...
- js中判断对象具体类型
大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...
- js中常用追加元素的几种方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript中判断对象类型方法大全1
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- JavaScript中判断对象类型的种种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- 转 JavaScript中判断对象类型的种种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?
1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...
- js中判断对象是否存在
s中判断对象是否存在,写法有很多种: 第一种:if (!myObj) { var myObj = { }; }第二种:var global = this; if (!global.myObj) { ...
- JS 中对变量类型的五种判断方法
5种基本数据类型:undefined.null.boolean.unmber.string 复杂数据类型:object. object:array.function.date等 方法一:使用typeo ...
随机推荐
- 1 vue 关键字解释
1 每一个计算属性都包含一个getter和一个setter,计算属性可以依赖其他计算属性,计算属性可以依赖当前vue实例的数据也可以依赖其他vue实例的数据 2 计算属性是基于它的依赖缓存的,方法则是 ...
- win7电脑数字键盘失灵怎么办
转自:https://zhidao.baidu.com/question/370674322729785324.html 解决方法: 第一步.同时按下“Windows键” + “R”调出运行窗口. 第 ...
- ASE19团队项目 beta阶段 model组 scrum3 记录
本次会议于12月4日,19时30分在微软北京西二号楼sky garden召开,持续20分钟. 与会人员:Jiyan He, Lei Chai, Linfeng Qi, Xueqing Wu, Yuto ...
- hashCode 及hashcode与equals的区别
1.hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值 详细了解请 参考 [1] public int hashCode()返回该对象的哈希码值.支持此方法是为了提高哈 ...
- java中i=i++的问题
java中 i = i++ 的结果 昨天看到下面这段代码,分享出来给大家看看,大家也可以讨论讨论. int i = 0; i = i++; System.out.println("i的值是 ...
- EtherNet/IP 协议应用层使用CIP协议&CIP协议中使用的TLS和DTLS(Network Infrastructure for EtherNet/IPTM: Introduction and Considerations)
- python爬虫伪装技术应用
版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82423865 -写在前面 本篇博客主要是爬虫伪装技术的应 ...
- 数列分段`Section II`(二分
https://www.luogu.org/problemnew/show/P1182 洛谷上的题目. 以后如果遇到1e5什么的 用二分试试! #include<iostream> # ...
- Til the Cows Come Home(Dijkstra)
Dijkstra (迪杰斯特拉)最短路算法,算是模板 POJ - 2387 #include<iostream> #include<algorithm> #include< ...
- CodeForces 768E SG函数 整数划分 Game of Stones
一个标准的NIM游戏 加上一条规则:每堆石子对于每个数目的石子只能被取一次 可以SG打表 dp[i][j]表示现在有i个石子 j是可以取的石子数的状压 第i位为1就表示i个石子没被取过 #includ ...