js type detection

js type detection
1. The typeof operator returns a string indicating the type of the uncalculated operand
Syntax: typeof operand
Parameters: operand is an expression representing an object or primitive value whose type will be returned

Types of
result
Undefined
"undefined"
Number
"number"
String
"string"
Boolean
"boolean"
Null
"object"
Object , Array
"object"
any other object
"object"

example:
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof 'abv' === 'string';
typeof (typeof 12) === 'string'; //typeof 总是返回一个字符串
typeof 1/0 === 'NaN';

2. The instanceof operator is used to test whether an object has a constructor's prototype property in its prototype chain.
Syntax: object instanceof constructor
Parameters: object The object to detect
constructor a constructor
Description: The instanceof operator is used to check whether constructor.prototype exists on the prototype chain of the parameter object.
example:
//定义构造函数
function C(){}
function D(){}

var o = new C();
o instanceof C; //true
o instanceof D; //false

3.isArray detection array
 
 
function isArray(obj) {      
      return Object.prototype.toString.call(obj) === '[object Array]';       
 }



I hope my experience of entering the pit is helpful to you, may the Holy Light be with you


Guess you like

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