美文网首页
JS数据类型的四种判断方式

JS数据类型的四种判断方式

作者: 小脚色 | 来源:发表于2018-12-27 17:19 被阅读0次

数据类型

基本类型:String、Number、Boolean、Undefined、Null 。由于其占据空间固定,是简单的数据段,为了便于提升变量查询速度,将其存储在栈中,即按值访问。

引用类型:Object。由于其值的大小会改变,所以不能将其存放在栈中,否则会降低变量查询速度,因此,其值存储在堆ject。由于其值的大小会改变,所以不能将其存放在栈中,否则会降低变量查询速度,因此,其值存储在堆(heap)中,而存储在变量处的值,是一个指针,指向存储对象的内存处,即按址访问。引用类型除 Object 外,还包括 Function 、Array、RegExp、Date 等等。

1. typeof

typeof("haha")  // "string"

typeof(222) // "number"

typeof(NaN) // "number"

typeof(true) // "boolean"

typeof(false) // "boolean"

typeof(null) // "object"

typeof(undefined) // "undefined"

typeof({}) // "object"

typeof([]) // "object"

typeof(function(){}) // "function"

typeof(()=>{}) //"function"

typeof(/2/) //"object"

typeof(new RegExp()) //"object"

typeof(new Date()) //"object"

无法识别出object、null、array、正则、日期等,

可以用于判断一个变量是否为函数,是的话,执行此函数,避免执行非函数导致程序报错。

2、instanceof

用来判断A是否为B的实例,语句:A instanceof B  如果 A 是 B 的实例,则返回 true,否则返回 false。

[] instanceof Array; //true

{} instanceof Object;//true

new Date() instanceof Date;//true

function Person(){};

new Person() instanceof Person;

[] instanceof Object; //true

new Date() instanceof Object;//true

需要注意的是instanceof是基于原型链的判断;

[].__proto__ === Array.prototype;

Array.prototype.__proto__ == Object.prototype;

所以[] instanceof Object为true

Date与正则同理。

综上所述:instanceof常用于判断变量是否为某个数据类型,例如判断某个变量(a)是否为数组  a  instanceof Array即可;

另外instanceof 不能用于基本数据类型的判断

3、constructor

{}.constructor === Object  //true

[].constructor === Array  //true

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

''.constructor === String  //true

var a = 2; a.constructor === Number  // true(2.constructor直接这么写浏览器会报变量错误)

true.constructor === Boolean //true

false.constructor ===Boolean //true

null.constructor  // Cannot read property 'constructor' of null

undefined.constructor // Cannot read property 'constructor' of undefined

其实[],{},'',2,false,true 均无constructor属性 都在各自的 __proto__中

根据上述打印结果:constructor 不可用于判断 null,undefined,且会报错

4.toString

Object.prototype.toString.call('') ;  // [object String]

Object.prototype.toString.call(1) ;    // [object Number]

Object.prototype.toString.call(true) ; // [object Boolean]

Object.prototype.toString.call(undefined) ; // [object Undefined]

Object.prototype.toString.call(null) ; // [object Null]

Object.prototype.toString.call(new Function()) ; // [object Function]

Object.prototype.toString.call(new Date()) ; // [object Date]

Object.prototype.toString.call([]) ; // [object Array]

Object.prototype.toString.call(new RegExp()) ; // [object RegExp]

Object.prototype.toString.call(new Error()) ; // [object Error]

Object.prototype.toString.call(document) ; // [object HTMLDocument]

Object.prototype.toString.call(window) ; //[object global] window是全局对象 global 的引用

此方法可以判断所有的数据类型,但是必须通过 call 或 apply 来调用,不能直接用,而不能直接调用 toString ,

虽然,从原型链的角度讲,所有对象的原型链最终都指向了 Object,

按照JS变量查找规则,其他对象应该也可以直接访问到 Object 的 toString方法,但是事实上,大部分的对象都实现了自身的 toString 方法,

这样就可能会导致 Object 的 toString 被终止查找,因此要用 call/apply 来强制调用Object 的 toString 方法。

例如:

Object.prototype.toString  === {}.toString  //true

Object.prototype.toString  === [].toString //false

相关文章

网友评论

      本文标题:JS数据类型的四种判断方式

      本文链接:https://www.haomeiwen.com/subject/zrldlqtx.html