Similarities and differences between instance and typeof in javascript

Usage of typeof

typeof is used to judge data types, which are divided into value types and reference types.

There are 4 types of value types: undefined, string, number, and boolean
. There are 2 types of reference types: function and object (it should be noted that typeof null==object)

Usage of instanceof

The instanceof operator is used to determine the reference type.

Syntax: A instanceof B (generally used to judge the relationship on the prototype chain)

Judgment rule: look along the line of A's proto property, and at the same time along the line of B's ​​prototype property, if the two lines can find the same reference, that is, the same object, return true.

E.g:

   alert( Object instanceof Function)    //true

   alert(Function  instanceof Object)    //true

   alert( Function instanceof Function)   //true

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652606&siteId=291194637