JS检测数据类型

  如果你要判断的是基本数据类型或JavaScript内置对象,使用toString; 如果要判断的时自定义类型,请使用instanceof

1.typeof

typeof操作符返回的是类型字符串,它的返回值有6种取值:

typeof 3 // "number"
typeof "abc" // "string"
typeof {} // "object"
typeof true // "boolean"
typeof undefined // "undefined"
typeof function(){} // "function"

所有对象的typeof都是"object",不能用于检测用户自定义类型。 比如DateRegExpArrayDOM Element的类型都是"object"

typeof []   // "object"

typeof还有一个知名的bug:

typeof null     // "object"

null是基本数据类型,它的类型显然是Null。其实这也反映了null的语义, 它是一个空指针表示对象为空,而undefined才表示什么都没有。 总之,typeof只能用于基本数据类型检测,对于null还有Bug。

2.instanceof

instanceof操作符用于检查某个对象的原型链是否包含某个构造函数的prototype属性。例如:

obj instanceof Widget

obj的原型链上有很多对象(隐式原型),比如:obj.__proto__obj.__proto__.__proto__, ...
如果这些对象里存在一个p === Widget.prototype,那么instanceof结果为true,否则为false

instanceof是通过原型链来检查类型的,所以适用于任何"object"的类型检查。

// 比如直接原型关系
function Animal(){ }
(new Animal) instanceof Animal     // true

// 原型链上的间接原型
function Cat(){}
Cat.prototype = new Animal
(new Cat) instanceof Animal         // true

instanceof也可以用来检测内置兑现,比如ArrayRegExpObjectFunction

[1, 2, 3] instanceof Array // true
/abc/ instanceof RegExp // true
({}) instanceof Object // true
(function(){}) instanceof Function // true

instanceof对基本数据类型不起作用,因为基本数据类型没有原型链。

3 instanceof Number // false
true instanceof Boolean // false
‘abc‘ instanceof String // false
null instanceof XXX  // always false
undefined instanceof XXX  // always false

但你可以这样:

new Number(3) instanceof Number // true
new Boolean(true) instanceof Boolean // true
new String(‘abc‘) instanceof String // true

但这时你已经知道数据类型了,类型检查已经没有意义了。

另外,instance还存在着跨窗口的问题

iWindow.document.write(‘<script> var arr = [1, 2]</script>‘);
iWindow.arr instanceof Array            // false
iWindow.arr instanceof iWindow.Array    // true

3.constructor

constructor属性返回一个指向创建了该对象原型的函数引用。需要注意的是,该属性的值是那个函数本身。例如:

function Animal(){}
var a = new Animal
a.constructor === Animal    // true

constructor不适合用来判断变量类型。首先因为它是一个属性,所以非常容易被伪造:

var a = new Animal
a.constructor == Array
a.constructor === Animal    // false

另外constructor指向的是最初创建当前对象的函数,是原型链最上层的那个方法:

function Cat(){}
Cat.prototype = new Animal

function BadCat(){}
BadCat.prototype = new Cat

(new BadCat).constructor === Animal   // true
Animal.constructor === Function       // true

instanceof类似,constructor只能用于检测对象,对基本数据类型无能为力。 而且因为constructor是对象属性,在基本数据类型上调用会抛出TypeError异常:

null.constructor         // TypeError!
undefined.constructor    // TypeError!

instanceof不同的是,在访问基本数据类型的属性时,JavaScript会自动调用其构造函数来生成一个对象。例如:

(3).constructor === Number // true
true.constructor === Boolean // true
‘abc‘.constructor === String // true
// 相当于
(new Number(3)).constructor === Number
(new Boolean(true)).constructor === Boolean
(new String(‘abc‘)).constructor === String

4.toString

toString方法是最为可靠的类型检测手段,它会将当前对象转换为字符串并输出。 toString属性定义在Object.prototype上,因而所有对象都拥有toString方法。 但ArrayDate等对象会重写从Object.prototype继承来的toString, 所以最好用Object.prototype.toString来检测类型。

toString = Object.prototype.toString;

toString.call(new Date);    // [object Date]
toString.call(new String);  // [object String]
toString.call(Math);        // [object Math]
toString.call(3);           // [object Number]
toString.call([]);          // [object Array]
toString.call({});          // [object Object]

// Since JavaScript 1.8.5
toString.call(undefined);   // [object Undefined]
toString.call(null);        // [object Null]

toString也不是完美的,它无法检测用户自定义类型。 因为Object.prototype是不知道用户会创造什么类型的, 它只能检测ECMA标准中的那些内置类型。

toString.call(new Animal)   // [object Object]

因为返回值是字符串,也避免了跨窗口问题。当然IE弹窗中还是有Bug,不必管它了。

Object.prototype.toString类似地,Function.prototype.toString也有类似功能, 不过它的this只能是Function,其他类型(例如基本数据类型)都会抛出异常。

判断window对象:

isWindow: function(obj){
    return obj && typeof obj === ‘object‘ && "setInterval" in obj;
}


时间: 2024-10-18 14:06:35

JS检测数据类型的相关文章

js中检测数据类型的几种方式

1.typeof 一元运算符,用来检测数据类型.只可以检测number,string,boolean,object,function,undefined. 对于基本数据类型是没有问题的,但是遇到引用数据类型是不起作用的(无法细分对象) let str = '{}'; let fn = function(){}; let obj = {}; let ary = []; let rg = /\d/; console.log(typeof str);//string console.log(typeo

js中的数据类型,以及如何检测数据类型

基本数据类型:string,number,boolean,null,undefined,symbol 引用数据类型:object(array,function...) 常用的检测数据类型的方法一般有以下三种: 1.typeof 一般主要用来检测基本数据类型,因为它检测引用数据类型返回的都是object 还需要注意的一点是:typeof检测null返回的也是object(这是JS一直以来遗留的bug) typeof 1 "number" typeof 'abc' "string

JS中检测数据类型的几种方式及优缺点【转】

1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string"."boolean"."undefined"."object"."function" 局限性:1)typeof null ->"object"2)检测的不管是数组还是正则都返回的是"obje

JS中检测数据类型的几种方式及优缺点

1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string"."boolean"."undefined"."object"."function" 局限性:1)typeof null ->"object"2)检测的不管是数组还是正则都返回的是"obje

3.2 js六大数据类型

js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型(Object). 前面说到js中变量是松散类型的,因此有时候我们需要检测变量的数据类型.typeof操作符可以检测变量的数据类型(输出的是一个关于数据类型的字符串).返回如下6种字符串:number,string,boolean,object,undefined,function.且看如下例子: var x = 1; console.log(typeof x

检测数据类型的四种方法

自己总结的四种检测数据类型的方法.如有不足,虚心接受大家的指点. 1.typeof检测基本数据类型的操作符 语法:result = typeof variable; 1)首先返回的都是字符串,其次字符串中为对应的数据类型.例如:“number”,“string”,“boolean”,“undefined”,“function”,“object” 局限性:不能具体细分是什么值,适合检测基本数据类型,不适合检测引用数据类型. typeof null -> "object" var s

js基本数据类型及判断方法

判断对象是否为空? Js代码 if (typeof myObj == "undefined") { var myObj = { }; } //这是目前使用最广泛的判断javascript对象是否存在的方法. 一.基本数据类型 js一共有六种数据类型:五种简单数据类型和一种复杂数据类型: 五种简单数据类型包括:String.Number.Boolean.undefined.Null 一种复杂数据类型:obeject 自己总结: 1,几种false undefined,null,空字符串,

通过JS检测360浏览器

如何通过JS检测360浏览器? 尝试了一大堆方法,网上大多数办法都是通过navigator.userAgent来判断,这可能在几年前是行得通的,现在360userAgent输出来跟谷歌除了版本号其余一模一样... 谷歌:      Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 360极速:Mozilla/5.0 (Windo

JS的数据类型

JS的数据类型大致分为两大块: 1.基本类型 2.引用类型 基本类型分为:number bolean string undefined null 引用类型分为:原生对象 内置对象 宿主对象