1. Methods for determining data types

1. typeof

Simple to use, but incomplete function, can not judge all data types, as shown in the following table:

type The results of
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
BigInt "bigint"
String "string"
Symbol "symbol"
Function "function"
Any other object "object"

2. toString

Use Object. The prototype. ToString. Correct judgment call () all data types, returns to form such as “[Object] XXX” string.

3. instanceof

The instanceof operator is used to check whether the constructor’s prototype property appears on the prototype chain of an instance object. Can’t be used to determine data types.

function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
}
const auto = new Car('Honda'.'Accord'.1998);

console.log(auto instanceof Car); // true
console.log(auto instanceof Object); // true
Copy the code

2. Encapsulate the judgment data type function

We can see the typeof data using typeof, but in JavaScript null, object {}, array [], regular expression /123/, and Date new Date() are all considered object, as shown in the following code:

console.log(typeof 123); // number
console.log(typeof 'string'); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
//----------------------------------------
console.log(typeof null); // object
console.log(typeof {}); // object
console.log(typeof []); // object
console.log(typeof /123/); // object
console.log(typeof new Date()); // object
Copy the code

If we want to enter an array [] and get array, we can’t do that using Typeof.

So how do you get array directly?

Let’s start with the following code:

console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call({})); // [object Object]
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call(/ 123 /)); // [object RegExp]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
Copy the code

As you can see, by the Object. The prototype. ToString. Call (), will need to judge the type of data in the call () get to characters, such as Null, Array, the RegExp, etc.

// Returns a string
console.log(typeof Object.prototype.toString.call([])); // string
Copy the code

Such as Object. The prototype. ToString. Call ([]) returns [Object Array], it is a string, so in addition to an Array of characters in the string will delete all can get Array.

// Delete all characters except Array from the string to get Array
console.log(Object.prototype.toString.call([]).replace('[object '.' ').replace('] '.' ')); // Array
Copy the code

To match the output form of typeof, convert the result returned to lowercase.

// Convert to lowercase
console.log(Object.prototype.toString.call([]).replace('[object '.' ').replace('] '.' ').toLowerCase()); // array
Copy the code

Finally, wrap the above procedure into its own function getType() :

const getType = obj= > {
    return typeof obj === 'object'
        ? Object.prototype.toString.call(obj).replace('[object '.' ').replace('] '.' ').toLowerCase()
        : typeof obj;
};
Copy the code

Check whether the typeof return value is ‘object’ and if so, do something about it. If not, return directly.

Verify:

console.log(getType(123)); // number
console.log(getType('string')); // string
console.log(getType(true)); // boolean
console.log(getType(undefined)); // undefined
console.log(getType(null)); // null
console.log(getType({})); // object
console.log(getType([])); // array
console.log(getType(/ 123 /)); // regexp
console.log(getType(new Date())); // date
Copy the code

Done!