码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 判断对象类型

时间:2014-05-24 07:18:35      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   


typeof

typeof是一个一元运算符,它返回的结果 始终是一个字符串,对不同的操作数,它返回不同的结果。

此表总结了typeof所有可能的返回值:
操作数类型 返回值
undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
函数对象 "function"
E4X XML 对象 "xml"
E4X XMLList 对象 "xml"
其他对象 "object"

 

 

 

 

 

 

 

 

 

bubuko.com,布布扣
// Numbers
typeof 37 === ‘number‘;
typeof 3.14 === ‘number‘;
typeof Math.LN2 === ‘number‘;
typeof Infinity === ‘number‘;
typeof NaN === ‘number‘; // Despite being "Not-A-Number"

// Strings
typeof "" === ‘string‘;
typeof "bla" === ‘string‘;
typeof (typeof 1) === ‘string‘; // typeof always return a string

// Booleans
typeof true === ‘boolean‘;
typeof false === ‘boolean‘;

// Undefined
typeof undefined === ‘undefined‘;
typeof blabla === ‘undefined‘; // an undefined variable

// Objects
typeof {a:1} === ‘object‘;
typeof [1, 2, 4] === ‘object‘; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
typeof new Date() === ‘object‘;

typeof new Boolean(true) === ‘object‘; // this is confusing. Don‘t use!
typeof new Number(1) === ‘object‘; // this is confusing. Don‘t use!
typeof new String("abc") === ‘object‘;  // this is confusing. Don‘t use!

// Functions
typeof function(){} === ‘function‘;
typeof Math.sin === ‘function‘;


typeof undefined;//"undefined"
typeof null;//"object" This stands since the beginning of JavaScript
typeof /s/ === ‘object‘; // Conform to ECMAScript 5.1
bubuko.com,布布扣

 

constructor

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

bubuko.com,布布扣
/*
* 通过constructor判断对象是否为Array对象
*/
var array = new Array();
array.constructor === Array; // true
bubuko.com,布布扣

instanceof

通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。

bubuko.com,布布扣
// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例
function Aoo(){} 
function Foo(){} 
Foo.prototype = new Aoo();//JavaScript 原型继承

var foo = new Foo(); 
console.log(foo instanceof Foo)//true 
console.log(foo instanceof Aoo)//true
bubuko.com,布布扣

上面的代码表示,在多层继承关系中,instanceof 运算符同样适用。

以下代码为 instanceof 代码实现。

bubuko.com,布布扣
/** JavaScript instanceof 运算符代码 **/
function instance_of(L, R) {//L 表示左表达式,R 表示右表达式
   var O = R.prototype;// 取 R 的显示原型
   L = L.__proto__;// 取 L 的隐式原型
   while (true) { 
     if (L === null) 
       return false; 
     if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true 
       return true; 
     L = L.__proto__; 
   } 
}
bubuko.com,布布扣
bubuko.com,布布扣
console.log(Object instanceof Object);//true 
console.log(Function instanceof Function);//true 
console.log(Number instanceof Number);//false 
console.log(String instanceof String);//false 

console.log(Function instanceof Object);//true 

console.log(Foo instanceof Function);//true 
console.log(Foo instanceof Foo);//false
bubuko.com,布布扣

 

 注:

 通过instanceof 和 constructor 在检测在跨框架(cross-frame)页面中的对象时,会失败。

原因就是在不同框架(iframe)中创建的数组不会相互共享其prototype属性。

正确的检测方法为:

bubuko.com,布布扣
var x = "";
Object.prototype.toString.call(x); // "[object String]"
        
var x = 1;
Object.prototype.toString.call(x); // "[object Number]"
        
var x = NaN;
Object.prototype.toString.call(x); // "[object Number]"
        
var x = Infinity;
Object.prototype.toString.call(x); // "[object Number]"
        
var x = new Array();
Object.prototype.toString.call(x); // "[object Array]"
        
var x = new Object();
Object.prototype.toString.call(x); // "[object Object]"
        
var x = new Date();
Object.prototype.toString.call(x); // "[object Date]"
        
var x = new Error();
Object.prototype.toString.call(x); // "[object Error]"
        
var x = RegExp();
Object.prototype.toString.call(x); // "[object RegExp]"
        
var x = undefined;
Object.prototype.toString.call(x); // "[object Undefined]"
        
var x = null;
Object.prototype.toString.call(x); // "[object Null]"
bubuko.com,布布扣
bubuko.com,布布扣
Object.prototype.toString.call(arguments); // "[object Arguments]"
bubuko.com,布布扣

jquery所提供的判定方法:

bubuko.com,布布扣
jQuery.isArray(x); //判定是否为数组
jQuery.isWindow(x); //判断是否为window对象,当前窗口和iframe
jQuery.isNumeric(x); //判定是否为数字 NaN,Infinity,""...false
jQuery.isFunction(x); //判定是否为javascript函数
bubuko.com,布布扣

 

 

 

javascript 判断对象类型,布布扣,bubuko.com

javascript 判断对象类型

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/chenqf/p/3734260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!