1. Use Typeof to judge
TypeOf is generally used to determine the underlying data type, as shown in the following example
let str = "123"
let num = 123
let bool = false
let undefin = undefined
let nul = null
let sym = Symbol('123')
let arr = [1, 23]
let obj = {
a: 1
}
function fn() {}
console.log(typeof (str)) // "string"
console.log(typeof (num)) // "number"
console.log(typeof (bool)) // "boolean"
console.log(typeof (undefin)) // "undefined
console.log(typeof (nul)) // "object"
console.log(typeof (sym)) // "symbol"
console.log(typeof (arr)) // "object"
console.log(typeof (obj)) // "object"
console.log(typeof (fn)) // "function"
Copy the code
Typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null) {typeof (null); Using typeOf does not distinguish objects from data. Both types return object. Next, we introduce another type criterion, which determines whether an object is an instance of a new object.
2. Use instanceOf for type judgment
The instanceof method is used to determine whether the new object is an object generated by the previous constructor, and thus the data type of the new object. Let’s look at the code
let Car = function() {}
let benz = new Car()
benz instanceof Car // true
let car = new String('Mercedes Benz')
car instanceof String // true
let str = 'Covid-19'
str instanceof String // false
Copy the code
Here is your own instanceof implementation
Function myInstanceof(left, right) {// Return false if(typeof left! == 'object' || left === null) return false; Let proto = object.getProtoTypeof (left); If (proto === null) return false; if(proto == null) return false; if(proto === right.prototype) return true; // Find the same prototype Object, return true proto = object.getProtoTypeof (proto); } // check myInstanceof console.log(myInstanceof(new Number(123), Number)); // true console.log(myInstanceof(123, Number));Copy the code
Both of the above methods have their own defects: Typeof can only be used to judge basic data types, and NULL cannot be used to judge complex data types accurately due to historical reasons. Instanceof can determine complex data types, but not the underlying data types.
3, the use of the Object. The prototype. ToString judgment to judge (ultimate)
ToString is a prototype method on Object. Calling this method returns “[Object Xxx]” (note that the first letter of the second word is uppercase). The Object team can call this method directly and return “[Object Object]”. If this method is called by another data type, it must be called by call to return the call correctly.
Object. The prototype. ToString ({}) / / "[Object Object]" Object. The prototype. ToString. Call ({}) / / same as above result, Plus the call also ok Object. The prototype. ToString. Call (1) / / "[Object Number]" Object. The prototype. ToString. Call (' 1 ') / / "[Object String]" Object.prototype.toString.call(true) // "[object Boolean]" Object.prototype.toString.call(function(){}) // "[object Function]" Object.prototype.toString.call(null) //"[object Null]" Object.prototype.toString.call(undefined) //"[object Undefined]" Object.prototype.toString.call(/123/g) //"[object RegExp]" Object.prototype.toString.call(new Date()) //"[object Date]" Object.prototype.toString.call([]) //"[object Array]" Object.prototype.toString.call(document) //"[object HTMLDocument]" Object.prototype.toString.call(window) //"[object Window]"Copy the code
By integrating the above 3 methods, the following ultimate judgment type function is obtained
function getType(obj){ let type = typeof obj; if (type ! == "object") {// Return type; } // If typeof returns object, Regular returns the return Object. The prototype. ToString. Call (obj). Replace (/ ^ \ [Object (\ S +) \] $/, "$1"); } /* Regex () {// Regex () {// regex () {// regex () {// regex () {// */ getType([]) // "Array" typeof [] is object, ToString returns getType('123') // "string" typeOF directly returns getType(window) // "window" toString returns getType(null) // "null" uppercase letter, Typeof NULL is object, GetType (undefined) // "undefined" typeof returns getType() // "undefined" typeof returns getType(function(){}) // "Function" typeof can determine, so lowercase getType(/123/g) //"RegExp" toString returnsCopy the code