JavaScript implements a function to determine the data type

Achieve effect

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

Implementation process

We can use typeofto check the data type, but in JavaScript null, objects {}, arrays [], regular expressions /123/, and dates new Date()are all considered to be 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

At this point, if we want to enter an array []and get arraysuch a return value directly , typeofit will not be possible to use it.

So how to get arrayit directly ?

Let's first look at the following piece of 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]

It can be seen by Object.prototype.toString.call()the data type of judgment put call()in can be obtained the desired character , such as Null, Array, RegExpand the like.

// 返回的是一个字符串
console.log(typeof Object.prototype.toString.call([])); // string

For example , if it is Object.prototype.toString.call([])returned [object Array], it is a string , then delete allArray the characters except for this string to get it .Array

// 字符串中除了 Array 以外的字符全删掉即可得到 Array
console.log(Object.prototype.toString.call([]).replace('[object ', '').replace(']', '')); // Array

In order to typeofbe consistent with the output format, the returned result can be converted to lowercase .

// 转化为小写
console.log(Object.prototype.toString.call([]).replace('[object ', '').replace(']', '').toLowerCase()); // array

Finally, encapsulate the above process into its own function getType():

function getType(obj) {
    
    
    return typeof obj === 'object' ? Object.prototype.toString.call(obj).replace('[object ', '').replace(']', '').toLowerCase() : typeof obj;
}

First judge whether the typeofreturn value is 'object', if it is , then it needs to be processed ; if it is not, just return directly .

verification

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

carry out!

Guess you like

Origin blog.csdn.net/Jack_lzx/article/details/111186210