day014: JS数据类型之问——检测篇

mac2024-02-18  53

day014: JS数据类型之问——检测篇

1. typeof 是否能正确判断类型?

对于原始类型来说,除了 null 都可以调用typeof显示正确的类型。

typeof 1 // 'number'

typeof '1' // 'string'

typeof undefined // 'undefined'

typeof true // 'boolean'

typeof Symbol() // 'symbol'

但对于引用数据类型,除了函数之外,都会显示"object"。

typeof [] // 'object'

typeof {} // 'object'

typeof console.log // 'function'

因此采用typeof判断对象数据类型是不合适的,采用instanceof会更好,instanceof的原理是基于原型链的查询,只要处于原型链中,判断永远为true

const Person = function() {}

const p1 = new Person()

p1 instanceof Person // true

var str1 = 'hello world'

str1 instanceof String // false

var str2 = new String('hello world')

str2 instanceof String // true

2. instanceof能否判断基本数据类型?

能。比如下面这种方式:

class PrimitiveNumber {

static [Symbol.hasInstance](x) {

return typeof x === 'number'

}

}

console.log(111 instanceof PrimitiveNumber) // true

如果你不知道Symbol,可以看看MDN上关于hasInstance的解释。

其实就是自定义instanceof行为的一种方式,这里将原有的instanceof方法重定义,换成了typeof,因此能够判断基本数据类型。

3. 能不能手动实现一下instanceof的功能?

核心: 原型链的向上查找。

function myInstanceof(left, right) {

//基本数据类型直接返回false

if(typeof left !== 'object' || left === null) return false;

//getProtypeOf是Object对象自带的一个方法,能够拿到参数的原型对象

let proto = Object.getPrototypeOf(left);

while(true) {

//查找到尽头,还没找到

if(proto == null) return false;

//找到相同的原型对象

if(proto == right.prototype) return true;

proto = Object.getPrototypeof(proto);

}

}

测试:

console.log(myInstanceof("111", String)); //false

console.log(myInstanceof(new String("111"), String));//true

4. Object.is和===的区别?

Object在严格等于的基础上修复了一些特殊情况下的失误,具体来说就是+0和-0,NaN和NaN。源码如下:

function is(x, y) {

if (x === y) {

//运行到1/x === 1/y的时候x和y都为0,但是1/+0 = +Infinity, 1/-0 = -Infinity, 是不一样的

return x !== 0 || y !== 0 || 1 / x === 1 / y;

} else {

//NaN===NaN是false,这是不对的,我们在这里做一个拦截,x !== x,那么一定是 NaN, y 同理

//两个都是NaN的时候返回true

return x !== x && y !== y;

}

最新回复(0)