js typeof instanceof and data types

js data type

JavaScript data types are divided into two types: primitive types (that is, basic data types) and object types (that is, reference data types) :
basic types: String, Number, Boolean, Null, Undefined, Symbol (es6)
reference types: Object, Array 、Function

one typeof

Used to describe the data type of the variable

typeof is an operator that can be used in two ways:

(1) typeof(expression);

(2) typeof variable name;

The return value is a string, used to describe the data type of the variable;

So you can use this to judge the seven types of number, string, object, boolean, function, undefined, symbol,

The content returned in each case is shown in the table below

 typeof has certain limitations: for instances of objects, arrays, null, and functions (new + function), the value returned is object .

For example, the values ​​returned by typeof(window), typeof(document), and typeof(null) are all object,

two instances of

Used to determine whether a variable is an instance of an object

The instanceof operator is used to detect whether the prototype attribute of the constructor appears on the prototype chain of an instance object ,

The return value is a Boolean value indicating whether a variable belongs to an instance of an object. Its syntax is as follows:

object instanceof constructor

The main implementation principle of instanceof is as long as the prototype of the variable on the right is on the prototype chain of the variable on the left .

Therefore, instanceof will traverse the prototype chain of the variable on the left during the search process until it finds the prototype of the variable on the right. If the search fails, it will return false.

instanceof can accurately judge complex reference data types, but cannot correctly judge basic data types . 

instanceof is used to judge objects, and can judge different instance objects. The judgment method is to query down in turn according to the prototype chain of the object. If the prototype attribute of obj2 exists in the prototype chain of obj1, the code form (obj1 instanceof obj2) (judgment obj1 is an instance of obj2), obj2 must be an object, otherwise an error will be reported. Returns a boolean value.

It can be seen that the above two methods have disadvantages and cannot meet the needs of all scenarios

If you need a general detection data type, you can use Object.prototype.toString, call this method, and uniformly return a string in the format “[object Xxx]”, the code form is as follows:

Object.prototype.toString({})       // "[object Object]"
Object.prototype.toString.call({})  // 同上结果,加上call也ok
Object.prototype.toString.call(1)    // "[object Number]"
Object.prototype.toString.call('1')  // "[object String]"
Object.prototype.toString.call(true)  // "[object Boolean]"
Object.prototype.toString.call(function(){})  // "[object Function]"
Object.prototype.toString.call(null)   //"[object Null]"
Object.prototype.toString.call(undefined) //"[object Undefined]"
Object.prototype.toString.call(/123/g)    //"[object RegExp]"
Object.prototype.toString.call(new Date()) //"[object Date]"
Object.prototype.toString.call([])       //"[object Array]"
Object.prototype.toString.call(document)  //"[object HTMLDocument]"
Object.prototype.toString.call(window)   //"[object Window]"

 

Guess you like

Origin blog.csdn.net/qq_30436011/article/details/129403170