This paper introduces data types in JavaScript and how to detect them
There are many data types in JavaScript, and in a consistent way, we classify these data types into two main categories, the original (basic) data type and the object type.
There are six basic (original) data types, null undefined boolean string number symbol, object type, object type including Object, Array, RegExp, Function, Error, Set, Map, WeakMap, etc.
There are four main ways to check the type of data in JavaScript code:
- typeof keyword
- instanceof keyword
- Object.prototype.toString.call()
- Constructor constructor properties
typeof keyword
The typeof keyword is the simplest and most common way to check data types.
console.log(typeof "abc") /* string */ console.log(typeof 12345) /* number */ console.log(typeof true) /* boolean */ console.log(typeof undefined) /* undefined */ console.log(typeof Symbol()) /* symbol */ console.log(typeof null) /* object */ console.log(typeof typeof typeof Symbol()) /* string */ console.log(typeof {}) /* object */ console.log(typeof []) /* object */ console.log(typeof /abc/) /* object */ console.log(typeof function(){}) /* function */
When we use the typeof keyword, there are a few things to note.
First, typeof null gets an object instead of a null, which is considered a design error in the JavaScript language. The reason why typeof null is judged an object is that null itself represents nothing, so it is represented as a series of zeros on a computer, whereas data read always starts with 000 when processed internally. That would be data that would be considered an object type.
Second, the typeof keyword obtains objects when it operates on object types. Only function types get special functions, that is, we cannot check whether a given data is an array, or a regular expression, etc. If you need to get the data types of these structures, or constructors to get them, the common way is to use the toString method on the Object prototype object, which always returns a string of [objectconstructor] structures.
Object.prototype.toString method
Object in JavaScript. Prototype. The toString method always returns a string of the [object constructor] structure, where the previous object in square brackets indicates that the data is of object type, followed by the constructor that created the instance. If it's a normal object, such as {name:'zs'}, the result of calling this method [code: ({name:'zs'}). toString()] is [object], but because of the override of the prototype chain method, arrays or other types of data cannot call this method directly. For data of object types other than ordinary objects, they need to be bound this by call or apply to invoke the method.
console.log(({}).toString()) /* [object Object] */ console.log(Object.prototype.toString.call({})) /* [object Object] */ console.log(Object.prototype.toString.call([])) /* [object Array] */ console.log(Object.prototype.toString.call(new Function)) /* [object Function]*/ console.log(Object.prototype.toString.call(new RegExp)) /* [object RegExp] */ console.log(Object.prototype.toString.call(new Error)) /* [object Error] */ console.log(Object.prototype.toString.call(new Set)) /* [object Set] */ console.log(Object.prototype.toString.call(new Map)) /* [object Map] */
Object in general. Prototype. The toString method is useful, and But is not everything, for example, it cannot handle type checks for custom types, including custom constructors and lass.
function Person() {}; let p = new Person; class Animal {}; let a = new Animal; console.log(Object.prototype.toString.call(p)) /* [object Object] */ console.log(Object.prototype.toString.call(a)) /* [object Object] */
For custom types of data, we need to check through instanceof or constructor.
instanceof keyword & constructor constructor properties
The instanceof keyword in the JavaScript language is used to check whether an object is an instance object created by a specified constructor, and when checked, the entire chain of prototypes is checked. If accurate, instanceof actually checks whether an object (the left value) is on the prototype chain of the specified constructor (the right value), returns true if it is, or false if it is not.
console.log(p instanceof Person) /* true */ console.log(a instanceof Person) /* false */ console.log(a instanceof Animal) /* true */ console.log(a instanceof Function, a instanceof Object) /* false true */ console.log(a.constructor == Person); /* false */ console.log(a.constructor == Animal); /* true */ console.log(a.constructor == Object); /* false */ console.log(p.constructor == Person); /* true */ console.log(({}).constructor == Object) /* true */ console.log(([]).constructor == Array) /* true */
Here we try to give the implementation code of the instanceof keyword, which is actually a dead-loop and prototype lookup within a loop.
/* instanceof Implementation Principle */ function mockInstanceof(instance,constructor){ let B = constructor.prototype; let A = instance.__proto__; while(true) { /* Object.prototype.__proto__ -> null */ if (A == null){ return false; } if (A === B){ return true; } A = A.__proto__; } } /* test data */ function Person() { }; function Boy() { }; let p = new Person; let b = new Boy; console.log(mockInstanceof(p, Person)) /* true */ console.log(mockInstanceof(p, Object)) /* true */ console.log(mockInstanceof(b, Boy)) /* true */ console.log(mockInstanceof(b, Person)) /* false */