js: Several ways to judge the data type

Reference article: Summary of several methods for judging object types in JavaScript

According to the data object to be judged, choose the appropriate method

1. Original data type

6种:number、string、boolean、undefined、null、symbol

1. typeof

let str = 'hello';
console.log(typeof str);	// string

【special! ! 】The type of null cannot be judged by typeof, instanceof, and constructor.
let s = null; console.log(typeof s) //Object

The following are two methods for judging whether it is a null type:
console.log(s === null)
console.log(Object.prototype.toString.call(s) === '[object Null]');

2. For some reference data types: object, function, typeof can also be used

let obj = {
    
    }
console.log(typeof obj);	// object
function func(){
    
    
	return 'this is a function';
}
console.log(typeof func);	// function

3. For other reference data types: Array, Date, RegExp

typeof has limitations: For some of the reference data types listed in the third case, using typeof can only display objects, not specific Arrays. [Cannot be used to distinguish: one object type from another object type]

let arr = [1,2,3,4];
console.log(typeof arr); // object,此时就不能typeof了

two, instanceof

let arr = [1,2,3,4];
console.log(arr instanceof Array);	// true

The instanceof operator requires that its left operand is an object, and its right operand is the name of the object's class or a constructor. The instanceof operator returns true if object is an instance of a class or constructor. Returns false if object is not an instance of the specified class or function, or if object is null.

let str = 'hello';
console.log(str instanceof String); // false

Three, constructor attribute

let arr = [1,2,3,4];
console.log(arr.constructor);	// ƒ Array() { [native code] },引用了初始化该对象的构造函数
console.log(arr.constructor === Array);	// true

let date = new Date();
console.log(typeof date);	// object
console.log(date.constructor);	// Date
let reg = new RegExp("/^/d*$/") ; 	
console.log(typeof reg);	// object
console.log(reg.constructor);	// RegExp

Supplement: What exactly is a JS prototype

The constructor attribute in the prototype object points to the constructor

[Thinking]: Is there any connection between the third and fourth methods?
Is arr a prototype object or an instance object?

4. For cross-framework

Let's understand the following passage by yourself, I don't understand it very well
insert image description here

四、Object.prototype.toString()

Reference: Usage of call(), apply(), bind() in JavaScript

call() is used to change the point of this, and apply and bind can be used in the same way

[Note]: bind returns a new function, which needs to be called again

let arr = [1,2,3,4];
console.log(Object.prototype.toString.call(arr));	// [object Array]
console.log(Object.prototype.toString.call(arr) === "[object Array]");

console.log(Object.prototype.toString.apply(arr));	
console.log(Object.prototype.toString.apply(arr) === "[object Array]");
console.log(Object.prototype.toString.bind(arr));	// ƒ toString() { [native code] }
console.log(Object.prototype.toString.bind(arr)());	//[object Array]
console.log(Object.prototype.toString.bind(arr)() === "[object Array]");

at last

Note : In order to describe intuitively, there is a situation where a variable is declared multiple times in the text. In the code, a variable declared with let can only be declared once, otherwise an error will be reported, but the variable value can be modified later

Guess you like

Origin blog.csdn.net/qq_38432089/article/details/124360034