Basic type checks are typeof, reference type checks are instanceof, and array.isarray () is used to check for arrays.

let a = "b" 
let b = true 
let c = '123'
let d = 123 
function e(){} 
let f = undefined
let g = null
let h = /[a-z]/
let i = {}
let k = [1.2.3]
Copy the code

Typeof running results

typeof a    'string'
typeof b    'boolean'
typeof c    'string'
typeof d    'number'
typeof e    'function'
typeof f    'undefined'
typeof g    'object'
typeof h    'object'
typeof i    'object'
typeof k    'object'
Copy the code

instanceof

e instanceof Function        true
h instanceof RegExp          true
i instanceof Object          true
k instanceof Array           true
Array.isArray(k)             true
Array.isArray(i)             false
Copy the code

Object.prototype.toString.call()

Object.prototype.toString.call(a)   '[object String]'
Object.prototype.toString.call(b)   '[object Boolean]'
Object.prototype.toString.call(c)   '[object String]'
Object.prototype.toString.call(d)   '[object Number]'
Object.prototype.toString.call(e)   '[object Function]'
Object.prototype.toString.call(f)   '[object Undefined]'
Object.prototype.toString.call(g)   '[object Null]'
Object.prototype.toString.call(h)   '[object RegExp]'
Object.prototype.toString.call(i)   '[object Object]'
Object.prototype.toString.call(k)   '[object Array]'
Copy the code

Remove the object

const _toString = function(val){
    return Object.prototype.toString.call(val).slice(8, -1)
}

_toString (a)   'String'
_toString (b)   'Boolean'
_toString (c)   'String'
_toString (d)   'Number'
_toString (e)   'Function'
_toString (f)   'Undefined'
_toString (g)   'Null'
_toString (h)   'RegExp'
_toString (i)   'Object'
_toString (k)   'Array'
Copy the code

Compared with typeof, instanceof, Array. IsArray Object. The prototype. ToString. Call more reliable, null, even it is detected

Webpack sources favor Typeof, Instanceof, and array. isArray, such as this snippet that parses the test, include, and exclude fields of a rule in module.rules. The values of these three fields can be strings, functions, regular expressions, and arrays.

Vue source code to do type detection, also like typeof, Instanceof and array.isarray (). Only when doing the type of the strict testing, will use to the Object. The prototype. ToString.

function isPrimitive(value){
  return (
    typeof value === "string" ||
    typeof value === "number" ||
    typeof value === "symbol" ||
    typeof value === "boolean"
    );
}
function isPromise(val){
  return (
      isDef(val) &&
      typeof val.then === "function" &&
      typeof val.catch === "function")}function isObject(obj){
  returnobj! = =null && typeof obj === "object";
}
 
/ / make strict type checking, would use the Object. The prototype. ToString
var _toString = Object.prototype.toString;
function toRawType(value){
  return _toString.call(value).slice(8, -1);
}
function isPlainObject(obj){
  return _toString.call(obj) === '[object Object]';
}
function isRegExp(v){
  return _toString.call(v) === '[object RegExp]';
}
Copy the code

Daily development, the typeof, instanceof and Array isArray () can satisfy the needs, the Object. The prototype. ToString. Call is more like the icing on the cake, but, Object. The prototype. ToString. Call native limitation, it take the non-native constructors do not have what way.

class Point{
	constructor(x,y){
		this.x = x;
		this.y = y;
	}
	toValue(){
		return this.x+this.y; }}let p = new Point(1.2);
Object.prototype.toString.call(p); Return "[object object]"
p instanceof Point;                / / return trueNote:ObjectThe.prototype.toString method can also be overridden.Copy the code

Type detection method has a typeof | instanceof | Array. The isArray | Object. The prototype. ToString. Call, “security” the type of Object detection method. The prototype. ToString. Call

Clone (number String Object Array Boolean); clone (number String Object Array BooleanCopy the code

ToString is an Object prototype, and Array, function, and so on are instances of Object that override toString. When the toString method is called by different object types, the corresponding overridden toString method is called based on the knowledge of the prototype chain (function returns a string containing the body of a function, Array returns a string consisting of elements……). ToString () does not get its Object type. Instead, it can only convert obj to a string. Therefore, when you want to get the concrete type of an Object, you should call the prototype toString method on Object. We can verify this by removing the toString method from the array

var arr=[1.12.13];
console.log(Array.prototype.hasOwnProperty("toString"));//true
console.log(arr.toString());/ / 1, 2, 3
delete Array.prototype.toString;The delete operator can delete instance attributes
console.log(Array.prototype.hasOwnProperty("toString"));//false
console.log(arr.toString());//"[object Array]"
Copy the code

demo

function getDataType(data){  
    var getType=Object.prototype.toString;  
    var myType=getType.call(data);// Call the call method to determine the type.
    var typeName=myType.slice(8, -1);// [object Function], that is, remove the string "[object".] "
    var copyInfo=' ';// The replicated data
    //console.log(data+" is "+myType);  
    switch(typeName){  
        case 'Number': copyInfo=data-0;  
                break;  
        case 'String': copyInfo="'"+data+"'";  
                break;  
        case 'Function': copyInfo=data;  
                break;  
        case 'Null': copyInfo=null;  
                break;  
        case 'Undefined': copyInfo="Undefined";  
                break;  
        case 'Array':   
                    copyInfo=[];// Make copyInfo an empty array first
                    for(var i=0; i<data.length; i++){ copyInfo[i]=data[i];// Write the data array data to copyInfo one by one
                    }  
                break;  
        case 'Object':   
                    copyInfo={};// First make copyInfo empty
                    for(var x in data){  
                        copyInfo[x]=data[x];  
                    }  
                break;  
        case 'Boolean': copyInfo=data;  
                break;  
        default : copyInfo=data;  
                break;  
    }  
    return copyInfo;  
} 
Copy the code