判断Javascript对象是否存在,及其类型

1.只判断对象是否存在

if (typeof myObj == "undefined") {

    var myObj = { };

  }

2.除了对象是否存在,还要判断对象是否有null值

if (!myObj) {

    var myObj = { };

  }

3.JavaScript中判断对象类型的种种方法

JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性:

1) typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串。如:"number","string","boolean","object","function","undefined"(可用于判断变量是否存在)。 但 typeof 的能力有限,其对于Array、Null、Date、RegExp类型返回的都是"object"。

if(typeof myObj == "undefined"){}

 2)instanceof 运算符。

instanceof 运算符要求其左边的运算数是一个对象,右边的运算数是对象类的名字或构造函数。如果 object 是 class 或构造函数的实例,则 instanceof 运算符返回 true。如果 object 不是指定类或函数的实例,或者 object 为 null,则返回 false。

所以,可以用instanceof运算符来判断对象是否为数组类型:

function isArray(arr){
    return arr instanceof Array;
}

3)constructor 属性。(typeof可以检查到变量是否有定义,而construct只能检查已定义变量的类型)

JavaScript中,每个对象都有一个constructor属性,它引用了初始化该对象的构造函数,常用于判断未知对象的类型。如给定一个求知的值 通过typeof运算符来判断它是原始的值还是对象。如果是对象,就可以使用constructor属性来判断其类型。所以判断数组的函数也可以这样写:

function isArray(arr){
    return typeof arr == "object" && arr.constructor == Array;
}
 
 

4)Object.prototype.toString.call([]);

在跨框架(cross-frame)页面中的数组时,在不同框架(iframe)中创建的数组不会相互共享其prototype属性。(constructor在类继承时会出错)。例如:

<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>

解决方法:

跨原型链调用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]";
}

为什么要用Object.prototype.toString而不是Function.prototype.toString或者其它?这是和他们的toString解释方式有关系的:

ECMA中对Object.prototype.toString的解释:

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)

其过程简单说来就是:1、获取对象的类名(对象类型)。2、然后将[object、获取的类名、]组合并返回。

 支付宝 JS 框架 base.js:

if (value instanceof Array ||
(!(value instanceof Object) &&
(Object.prototype.toString.call((value)) == '[object Array]') ||
typeof value.length == 'number' &&
typeof value.splice != 'undefined' &&
typeof value.propertyIsEnumerable != 'undefined' &&
!value.propertyIsEnumerable('splice'))) {
return 'array';
}

扩展一下,用于检测各种对象类型:

 
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
原文地址:https://www.cnblogs.com/peng14/p/2653953.html