Main types of judgment methods:

typeof  
instanceof  
* Object.prototype.toString.call() 
* Array.isArray()
Copy the code
Typeof determines only basic types :number String object undefined BooleanCopy the code

typeof

console.log(typeof(2),'test')  //number
console.log(typeof(true),'test') //boolean
console.log(typeof('2'),'test') //string
console.log(typeof([]),'test') //object
console.log(typeof({}),'test') //object
console.log(typeof(null),'test') //object
console.log(typeof(undefined),'test')
//undefined
function f() {}
console.log(typeof(f),'test') //function
Copy the code

Typeof has no way of determining reference types

Instanceof (think it is simple, but the most complicated)

Checks whether the variable is an instance of an object and returns the valuetrueorfalse
Copy the code
let o = {}
console.log(o instanceof Object)  //true
let a = []
console.log(a instanceof Array) //true
function f() {}
console.log(f instanceof Function) //true
console.log(true instanceof Boolean) //false
console.log(1 instanceof Number) //false
console.log('1' instanceof String) //false
Copy the code

You can see that when it comes to value types, it says “not normal.”

var str = new String('000')
var str2 = '000'
console.log(str1 instanceof String) //true
console.log(str2 instanceof String)
//false
console.log(str) //String{"000"} 
console.log(str2) //000
Copy the code

You can see that there are different ways to create strings, but instanceof is different, right?

So how does Instanceof work?

This operator is defined by the specification
Function instance_of(L, R) {var O = r.protoType; // take the display prototype of R L = l.__proto__; While (true) {if (L === null) return false; If (O === L)// return true when O is exactly L; L = L.__proto__; }}Copy the code
Returns only if the implicit and explicit types of the data type are absolutely equaltrue
Copy the code

That’s where the prototype chain comes in

You can create strings either way, but STR creates a new space in the heap for new objects and has a stereotype chain, while STR2 is placed in the constant pool (no stereotype chain).

1. If the string constant "AAA" already exists in the constant pool, the program will search for the string "AAA" in the constant pool and assign the address of the string "AAA" to A. By creating an object in method one, the program creates a new space in heap memory to hold the new object, regardless of whether or not the "AAA" string is in the constant pool. 2. If there is no string constant "AAA" in the constant pool, the program will put the string "AAA" into the constant pool and assign its address to A. By creating an object in method one, the program creates a new space in the heap to hold the new object and puts the "AAA" string into the constant pool, creating two objects.Copy the code

So new will come out with a stereotype property. Direct assignment doesn’t.

Instanceof is suitable for a single global execution environment, but not for two or more different execution environments, and is suitable for array. isArray detection

Array.isArray()

letArr = [1, 3] console.log(array.isarray (arr)) //true
Copy the code

Object.prototype.toString.call()

console.log(Object.prototype.toString.call('1')) 
console.log(Object.prototype.toString.call(1)) 
console.log(Object.prototype.toString.call(true))
console.log(Object.prototype.toString.call(null)) 
console.log(Object.prototype.toString.call(undefined))
console.log(Object.prototype.toString.call([]))
var set = new Set();
console.log(Object.prototype.toString.call(set))
var map = new Map();
console.log(Object.prototype.toString.call(map))
console.log(Object.prototype.toString.call({}))
function f2(){}
console.log(Object.prototype.toString.call(f2))


//output
[object String]
[object Number]
[object Boolean]
[object Null]
[object Undefined]
[object Array]
[object Set] 
[object Map]
[object Object]
[object Function]
Copy the code
So use the Object. The protoType. ToString. Call () more accuratelyCopy the code

Remember the Object. The protoType. The toString () to check types are Object, so want to use the Object. The protoType. ToString. Call ()