Home >Web Front-end >JS Tutorial >Judgment type in JS

Judgment type in JS

不言
不言Original
2018-04-10 11:38:334089browse

The content shared with you in this article is about the judgment type in JS, which has certain reference value. Friends in need can refer to it

There are generally four methods to judge the type in JS
1,
typeof: As everyone knows about this method, there are many types that cannot be determined, and it is not recommended.
2,
constructor: Each function will automatically have a prototype attribute. This property is an object, and this object contains the only non-enumerable property constructor. The value of the constructor attribute is a function object, and this function is the constructor;

function Aaa(){};
var a1 = new Aaa();

a1.constructor points to It is Aaa(); this constructor

So we can use it to do type judgment

var arr = [];
arr.constructor == Array This way we can judge whether it is Array, the same is true for other types


instanceof: Determine whether an object and the constructor are on a prototype chain;

function Aaa(){};
var o = new Aaa();

o instanceof Array : Check whether o and Array are on the same prototype chain. Obviously o is an instance under the Aaa constructor, so we get false;

o instanceof obj.prototype gets true; because all objects inherit obj.prototype

4,
Use the toString() method: Object.prototype.toString.call(o);
o : is an object instance. The above will return a string of type '[Object Array]'. You can judge the data type based on this string.

Summary, the fourth general method is the most reliable and safe. , Methods two and three generally have no problems, but they will go wrong when there is an iframe. One is the most unreliable

There are generally four methods to determine the type in JS
One,
typeof: Everyone knows this method. There are many types that cannot be determined. It is not recommended.
2.
constructor: Each function will automatically have a prototype attribute. This property is an object, and this object contains the only non-enumerable property constructor. The value of the constructor attribute is a function object, and this function is the constructor;

function Aaa(){};
var a1 = new Aaa();

a1.constructor points to It is Aaa(); this constructor

So we can use it to do type judgment

var arr = [];
arr.constructor == Array This way we can judge whether it is Array, the same is true for other types


instanceof: Determine whether an object and the constructor are on a prototype chain;

function Aaa(){};
var o = new Aaa();

o instanceof Array : Check whether o and Array are on the same prototype chain. Obviously o is an instance under the Aaa constructor, so we get false;

o instanceof obj.prototype gets true; because all objects inherit obj.prototype

4,
Use the toString() method: Object.prototype.toString.call(o);
o : is an object instance. The above will return a string of type '[Object Array]'. You can judge the data type based on this string.

Summary, the fourth general method is the most reliable and safe. , the second and third methods generally have no problems, but they will go wrong when there is an iframe. The first is the most unreliable

Related recommendations;

js judgment is on the PC side Or mobile terminal

js determine whether it is PC terminal or mobile terminal

The above is the detailed content of Judgment type in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:JS data structureNext article:JS data structure