likes
comments
collection
share

JavaScript中数据类型的检测方法及其特点

作者站长头像
站长
· 阅读数 15

JS中数据类型的检测方法及其特点

检测数据类型的方法:typeof、instanceof、constructor、Objec.prototype.toString,下面我们分别来讲讲这些方法的异同以及什么情况下使用什么方法。

1.typeof

介绍:用来检测数据类型,但是只能准确检测基本数据类型。

返回的结果:number、boolean、string、object(Array等引用类型)undefined、function。 在检测null的时候也会返回object,这是由于JavaScript最初设计的时候出现的一个bug。

在JavaScript最初的实现中,JavaScript中的值是由一个表示类型的标签和实际数据值表示的。对象类型标签是0。由于null代表的是空指针(大多数平台下值为0x00),因此,null的类型标签也成了0,typeof null就错误的返回了"object"

(以下例子的结果都为true
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写
typeof Number(1) === 'number'; // 但不要使用这种形式!

// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof总是返回一个字符串
typeof String("abc") === 'string'; // 但不要使用这种形式!

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // 但不要使用这种形式!

// Symbols
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol';

// Undefined
typeof undefined === 'undefined';

// Objects
typeof {a:1} === 'object';

// 使用Array.isArray 或者 Object.prototype.toString.call
// 区分数组,普通对象
typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';

// 下面的容易令人迷惑,不建议使用
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';

// 函数
typeof function(){} === 'function';
typeof class C{} === 'function'
typeof new Function() === 'function';

2.instanceof

介绍:用于测试构造函数的prototype属性出现在对象的原型链中的任何位置。可以用于检测某个对象是否为另一个对象的实例,但是无法检测基本数据类型的值。

let num = 123;
num instanceof Number // false

let str = "test"
str instanceof String // false

str = new String
str instanceof String // true

3.constructor

介绍:用来查看对象的数据类型。

构造函数.constructor.prototype = 数据类型

let number = 123
number.constructor === Number // true
let str = "123"
str.constructor === String // true

注意:

1.undefined和null是不能够判断出类型的,并且会报错。因为null和undefined是无效的对象 2.使用constructor是不保险的,因为constructor属性是可以被修改的,会导致检测出的结果不正确

let nul = null;
nul.constructor == Object //报错
let und = undefined;
und.constructor == Object //报错
function test() {}
test.constructor === Function // true
test.constructor === String // false
test.constructor = String 
test.constructor === Function // false
test.constructor === String // true

4.Object.prototype.toString.call(实例)

介绍:找到Object原型上的toString方法,让方法执行,并且让方法中的this变指向实例(实例就是我们要检测数据类型的值)

Object.prototype.toString判断对象值属于哪种内置属性,他返回一个JSON字符串--"[Object 数据类型]"(可以判断所有的数据类型)

示例:
let nul = null;
Object.prototype.toString.call(nul) //[object Null]
let und = undefined;
Object.prototype.toString.call(und) //[object Undefined]
let error = new Error();
Object.prototype.toString.call(error) //[object Error]

总结

从上面的学习之中我们可以知道,一般js在检测数据类型时可以使用typeof和instanceof的组合或单独使用Object.prototype.toString.call,由于constructor的不确定性,所以不建议使用。

typeofinstanceofconstructorObject.prototype.toString.call
能准确检测基础类型能够准确检测引用类型能准确检测大部分类型(null和undefined无法检测)能检测所有类型
使用简单使用简单使用较复杂,有伪造风险使用较复杂
无法准确检测引用类型和null无法准确检测基础类型constructor易被修改IE6下,undefined和null均为Object