登录  /  注册
首页 > web前端 > js教程 > 正文

JavaScript专题之六:类型检测

coldplay.xixi
发布: 2021-03-09 09:47:06
转载
2057人浏览过

javascript专题之数据类型检测的那些事

JavaScript专题之六:类型检测

目录

  • 一、typeof
  • 二、instanceof
  • 三、constructor
  • 四、stringTag是什么?
  • 五、实现几个数据检测的方法
  • 写在最后

(免费学习推荐:javascript视频教程

前言

在《JavaScript的数据类型》中我们也提到过简单的类型检测问题。

作为前端的同学,我们应该都知道可以使用typeof和instanceof在运行时判断JavaScript数据的类型,那么他们都是怎么判断的呢?一千个人会不会写出来一千个判断方法?

本文会从通用的typeof、到专攻对象的instanceof,再到isNull、isNumber、isString等方法,来讨论如何判断数据类型,一起加油~

在这里插入图片描述

一、typeof

typeof:操作符返回一个字符串,表示未经计算的操作数的类型。

我们都知道,在 ES6 前,JavaScript 共六种数据类型,分别是:

  1. Undefined
  2. Null
  3. Boolean
  4. Number
  5. String
  6. Object

然而当我们使用 typeof 对这些数据类型的值进行操作的时候,返回的结果却不是一一对应,分别是:

  1. Undefined
  2. object – ***
  3. Boolean
  4. Number
  5. String
  6. Object

有一些意外,typeof null => 'object'typeof function(){} => 'function'

所以在大多数情况下我们可以使用typeof来检测基本数据类型,但是,检测得到的Object后,却无法区分是哪一种Object:

typeof [] === 'object'; //truetypeof {} === 'object'; //truetypeof null === 'object'; //true
登录后复制

总结

在检测Js的原始类型时,除了typeof null返回object之外,其他的都返回对应类型名的小写字母。

二、instanceof

我们判断对象类型的时候,可以使用instanceof:

如果对原型及原型链不太熟悉的同学不妨看看这篇文章从原型到原型链

定义

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

实例

const arr = [];const obj = {};console.log(arr instanceof Array);   // trueconsole.log(arr instanceof Object);  // trueconsole.log(obj instanceof Array);   // falseconsole.log(obj instanceof Object);  // true
登录后复制

注意instanceof是能匹配类型的父类的,所以arr instanceof Array和arr instanceof Object都是true,因为Object是Array的父类。

满足class extends原型链规则的父子类关系的对象都能被匹配:

class

class Base {}class Current extends Base {}const obj = new Current();console.log(obj instanceof Current); // trueconsole.log(obj instanceof Base); // true
登录后复制

原型链

function Foo() {}function Bar() {}Bar.prototype = new Foo();const obj = new Bar();console.log(obj instanceof Bar); // trueconsole.log(obj instanceof Foo); // true
登录后复制

注意如果我们修改obj的原型链能改变instanceof的结果:

function Other() {}obj.__proto__ = new Other();console.log(obj instanceof Other); // trueconsole.log(obj instanceof Foo); // false
登录后复制

总结

instanceof借助了原型链来判断的实际上,只要一个类型Type的prototype在一个对象obj的原型链上,那么obj instanceof Type就是true,否则就是false。

三、constructor

有时候我们不希望匹配父类型,只希望匹配当前类型,那么我们可以用constructor来判断:

如果对原型及原型链不太熟悉的同学不妨看看这篇文章从原型到原型链

定义

返回创建实例对象的 Object 构造函数的引用。注意,此属性的值是对函数本身的引用,而不是一个包含函数名称的字符串。

实例

const arr = [];console.log(arr.constructor === Array); // trueconsole.log(arr.constructor === Object); // false
登录后复制

对象的constructor会返回它的类型,而类型在定义的时候,会创建一个name只读属性,值为类型的名字。

class Foo {}console.log(Foo.name); // Fooconst foo = new Foo();console.log(foo.constructor === Foo); // trueconsole.log(foo.constructor.name === 'Foo'); // true
登录后复制

疑问

  • constructor.name 一定就是正确的吗?
  • 我们可以修改它吗?

四、stringTag是什么?

4.1 stringTag——类型标签

如果你看过一些库的早期实现,你会发现使用Object.prototype.toString来做类型判断的方式,他们会数据类型的字符串标志,被称作stringTag

4.2 Object.prototype.toString

定义

toString()方法返回一个表示该对象的字符串。

每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,默认情况下,toString() 方法被每个 Object 对象继承。

如果此方法在自定义对象中未被覆盖,toString() 返回 “[object type]”,其中 type 是对象的类型。以下代码说明了这一点:

实例

比如这是requirejs里面的代码片段。

var ostring = Object.prototype.toString;function isArray(it) {
  return ostring.call(it) === '[object Array]';}
登录后复制

toString时都做了什么?

这里直接将冴羽大大的总结搬了过来:

When the toString method is called, the following steps are taken:

  1. If the this value is undefined, return “[object Undefined]”.
  2. If the this value is null, return “[object Null]”.
  3. Let O be the result of calling ToObject passing the this value as the argument.
  4. Let class be the value of the [[Class]] internal property of O.
  5. Return the String value that is the result of concatenating the three Strings "[object ", class, and “]”.

当 toString 方法被调用的时候,下面的步骤会被执行:

  1. 如果 this 值是 undefined,就返回 [object Undefined]
  2. 如果 this 的值是 null,就返回 [object Null]
  3. 让 O 成为 ToObject(this) 的结果
  4. 让 class 成为 O 的内部属性 [[Class]] 的值
  5. 最后返回由 "[object " 和 class 和 “]” 三个部分组成的字符串

注意

有几点我们需要注意:

  • toString无法区分原始类型及其构造对象;
    • 我们认为Number、Boolean这种类型在被构造器构造出来后的类型应该是对象
    • 但toString都会返回[object number]等原始类型;
  • toString方法是可以自定义的;

在这里插入图片描述

五、实现几个数据检测的方法

好了看了几款常用的类型判断方法后,我们可不可以实现自己的类型判断工具?就利用上述提到的一个或多个方法。我们自己动手丰衣足食~

5.1 isObject

注意,我们认为null不是一个对象,它就是null~

function isObject(value) {
    const type = typeof value;
    return value != null && (type === 'object' || type === 'function');}
登录后复制
5.2 isNull
function isNull(value) {
    return value === null;}
登录后复制
5.3 isFunction
function isFunction(value) {
    return typeof value === 'function';}
登录后复制
5.4 isArray
var isArray = Array.isArray || function( value ) {
    return type(value) === "array";}
登录后复制
5.5 stringTag
const toString = Object.prototype.toString;function getTag(value) {
    // if (value === null) return '[object Null]';
    // if (value == null) return '[object Undefined]'
    if (value == null) {
        return value === undefined ? '[object Undefined]' : '[object Null]'
    }
    return toString.call(value)}
登录后复制

好了到最后,大家平时对类型检测的态度是什么样的呢?

相关免费学习推荐:javascript(视频)

以上就是JavaScript专题之六:类型检测的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号