The difference between typeof and instanceof

Example:

let a = 1
let b = 'ss'
let c
let d = null
let e = true
let f = {
    
    }
let g = function () {
    
    

}
console.log(typeof a)
console.log(typeof b)
console.log(typeof c)
console.log(typeof d)
console.log(typeof e)
console.log(typeof f)
console.log(typeof g)

Result:
Insert picture description here
The function of instanceof is whether the __proto__ of the preceding variable and the prototype of the following variable are crossed. Refer to the following figure for details.
Insert picture description here
Example:

function Foo() {
    
    

}
let a = new Foo()
console.log(a instanceof Object)

Result: In
Insert picture description here
this example, a.__proto__ and Object.prototype are the same, so return true
Insert picture description here

Guess you like

Origin blog.csdn.net/plan_jok/article/details/112522947