javascript专题之数据类型检测的那些事
目录
一、typeof二、instanceof三、constructor四、stringtag是什么?五、实现几个数据检测的方法写在最后
(免费学习推荐:javascript视频教程)
前言
在《javascript的数据类型》中我们也提到过简单的类型检测问题。
作为前端的同学,我们应该都知道可以使用typeof和instanceof在运行时判断javascript数据的类型,那么他们都是怎么判断的呢?一千个人会不会写出来一千个判断方法?
本文会从通用的typeof、到专攻对象的instanceof,再到isnull、isnumber、isstring等方法,来讨论如何判断数据类型,一起加油~
一、typeof
typeof:操作符返回一个字符串,表示未经计算的操作数的类型。
我们都知道,在 es6 前,javascript 共六种数据类型,分别是:
undefinednullbooleannumberstringobject然而当我们使用 typeof 对这些数据类型的值进行操作的时候,返回的结果却不是一一对应,分别是:
undefinedobject – ***booleannumberstringobject有一些意外,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:
if the this value is undefined, return “[object undefined]”.if the this value is null, return “[object null]”.let o be the result of calling toobject passing the this value as the argument.let class be the value of the [[class]] internal property of o.return the string value that is the result of concatenating the three strings [object , class, and “]”.当 tostring 方法被调用的时候,下面的步骤会被执行:
如果 this 值是 undefined,就返回 [object undefined]如果 this 的值是 null,就返回 [object null]让 o 成为 toobject(this) 的结果让 class 成为 o 的内部属性 [[class]] 的值最后返回由 [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 isnullfunction isnull(value) { return value === null;}
5.3 isfunctionfunction isfunction(value) { return typeof value === 'function';}
5.4 isarrayvar isarray = array.isarray || function( value ) { return type(value) === array;}
5.5 stringtagconst 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专题之六:类型检测的详细内容。