Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to determine the data type in js
How to determine the data type in js

How to judge the data type in js: typeof, instanceof, constructor, prototype method comparison

How to judge the type in js, here are a few examples:

< p>var a = "iamstring.";

var b = 222;

var c= ;

var d = new Date();

p>

var e =

function(){alert(111);};

var f =

function(){this.name ="22";};

The most common judgment method: typeof

alert(typeof a)

---------- --> string

alert(typeof b)

------------> number

alert(typeof c)< /p>

------------> object

alert(typeof d)

---------- -> object

alert(typeof e)

------------> function

alert(typeof f)

------------> function

The types returned by typeof are all in string form. Please note that, for example:

alert (typeof a == "string")

-------------> true

alert(typeof a == String)

---------------> false

In addition, typeof

can determine the type of function; when determining objects other than Object type More convenient.

Method to determine known object type: instanceof

alert(c instanceof Array)

------------- -> true

alert(d instanceof

Date)

alert(f instanceof Function)

------- -----> true

alert(f instanceof function)

------------> false

Note: instanceof

must be followed by an object type, and the case cannot be wrong. This method is suitable for some conditional selection or branching.

Judge based on the constructor of the object:

constructor

alert(c.constructor ===

Array) ----- -----> true

alert(d.constructor === Date)

-----------> true

alert(e.constructor ===

Function) -------> true

Note: constructor will cause errors when inheriting classes

eg,

function A(){};

function B(){};

A.prototype = new B(); //A inheritance Since B

var aObj = new A();

alert(aobj.constructor === B) ----------->

true;

alert(aobj.constructor === A) ----------->

false;

This problem will not occur with the instanceof method. Both direct and indirect inheritance of objects will report true:

alert(aobj instanceof B) ---------------->

true;

alert(aobj instanceof B) ---------------->

true;

p>

Getting back to the subject, to solve the problem of construtor is usually to let the object's constructor point to itself manually:

aobj.constructor = A;

//Assign your own class to the object The constructor attribute

alert(aobj.constructor === A) ----------->

true;

alert( aobj.constructor === B) ----------->

false; //The base class will not report true;

General but very cumbersome Method: prototype

alert(Object.prototype.toString.call(a) === '[object String]')

-------> true;

alert(Object.prototype.toString.call(b) === '[object Number]')

-------> true;

alert(Object.prototype.toString.call(c) === '[object Array]')

-------> true;

alert (Object.prototype.toString.call(d) === '[object Date]')

-------> true;

alert(Object.prototype .toString.call(e) === '[object Function]')

-------> true;

alert(Object.prototype.toString.call (f) === '[object Function]')

-------> true;

You can’t write wrong capitalization, which is more troublesome, but it’s better in general use .

Usually, you can use typeof

to judge. If you encounter a situation where the Object type is known, you can use the instanceof or constructor method