博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript中的数据类型及其检测
阅读量:7231 次
发布时间:2019-06-29

本文共 2426 字,大约阅读时间需要 8 分钟。

JavaScript中的数据类型及其检测

1. 数据类型

1.1 基本类型

  • Number

  • String

  • Boolean

  • Null

  • Undefined

  • Symbol

1.2 引用类型

  • Object

  • Array

  • Function

  • RegExp

  • Date

2. 类型检测

2.1 typeof

var s = 'Nicholas';var b = true;var i = 22;var u;var n = null;var o = new Object();var f = new Function();console.info(typeof s);     // stringconsole.info(typeof b);     // booleanconsole.info(typeof i);     // numberconsole.info(typeof u);     // undefinedconsole.info(typeof n);     // objectconsole.info(typeof o);     // objectconsole.info(typeof f);     // function

typeof只能检测基本数据类型,对于null还有一个Bug

2.2 instanceof

result = variable instanceof constructor

instanceof用于检测某个对象的原型链是否包含某个构造函数的prototype属性

function C() {}function D() {}var o = new C();o instanceof C     // true, Object.getPrototypeOf(o) === C.prototypeo instanceof D     // falsefunction Animal() {}function Cat() {}Cat.prototype = new Animal();var b = new Cat();b instanceof Animal     // true[1, 2, 3] instanceof Array      // true/abc/ instanceof RegExp     // true({}) instanceof Object      // true(function() {}) instanceof Function     // true

instanceof适用于检测对象,它是基于原型链运作的

2.3 constructor

constructor属性返回一个指向创建了该对象原型的函数引用,该属性的值是哪个函数本身。

function Animal() {}function Cat() {}function BadCat() {}Cat.prototype = new Animal();BadCat.prototype = new Cat();var a = new Animal();a.constructor === Animal    // truevar b = new BadCat();b.constructor === Animal    // true

constructor指向的是最初创建者,而且易于伪造,不适合做类型判断

2.4 toString

Object.prototype.toString.call();({}).toString.call();window.toString.call();toString.call();
Object.prototype.toString.call([]);     // [object Array]Object.prototype.toString.call({});     // [object Object]Object.prototype.toString.call('');     // [object String]Object.prototype.toString.call(new Date());     // [object Date]Object.prototype.toString.call(1);      // [object Number]Object.prototype.toString.call(function () {});     // [object Function]Object.prototype.toString.call(/test/i);    // [object RegExp]Object.prototype.toString.call(true);       // [object Boolean]Object.prototype.toString.call(null);       // [object Null]Object.prototype.toString.call();       // [object Undefined]

几乎十全十美,只是不能检测用户自定义类型

2.5 小结

  • typeof只能检测基本数据类型,对于null还有Bug

  • instanceof适用于检测对象,它是基于原型链运作的

  • constructor指向的是最初创建者,而且容易伪造,不适合做类型判断

  • toString适用于ECMA内置JavaScript类型(包括基本数据类型和内置对象)的类型判断

  • 基于引用判等的类型检查都有跨窗口问题,比如instanceofconstructor

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

参考

转载地址:http://dyvfm.baihongyu.com/

你可能感兴趣的文章
cs231n 17-18 assignment2 出现 No module named 'past' 解决方法
查看>>
Ajax基础
查看>>
全面理解 git
查看>>
Activiti Modeler初探实践
查看>>
NET Core Kestrel部署HTTPS使用SSL证书
查看>>
20165320 结对编程学习第一周
查看>>
[转] 池子法 邻接表建图
查看>>
【前端积累】SEO 学习
查看>>
tt安装与配置
查看>>
software testing HW02
查看>>
linux .net mono方案测试记录与报告(一)
查看>>
某未被少林寺吞并的小寺庙师徒的经典对话
查看>>
IT兄弟连 JavaWeb教程 JSP内置对象1
查看>>
IT兄弟连 JavaWeb教程 JSP内置对象经典案例
查看>>
XCode环境变量及路径设置 解决头文件找不到的问题
查看>>
[Go]链表的相关知识
查看>>
C# Json数据反序列化为Dictionary并根据关键字获取指定值1
查看>>
jS Ajax 上传文件报错"Uncaught TypeError: Illegal invocation"
查看>>
javascript、jquery获取网页的高度和宽度
查看>>
面向对象---代码练习(以车为案例)
查看>>