Judging the type in JS: Typeof/instanceof/constructor/prototype

Source: Internet
Author: User

How to judge the type in JS, first to give a few examples:

var a = "Jason";

var B = 123;

var C = true;

var d = [n/a];

var e = new Date ();

var f = function () {
Alert (' Jason ');

};


First, the most common method of judging: typeof

typeof is a unary operator, and the result returned is always a string, with different operands, it returns different results, another typeof can determine the type of function, and it is more convenient to judge objects of type object.

Console.log (typeof a = = "string"); True

Console.log (typeof a = = String); False
The specific rules are as follows:

1) for operands of numeric types, the value returned by TypeOf is number. For example: typeof 1, the value returned is number.

The above is the general number, and for the unconventional numeric type, the result returns the number. typeof Nan,nan, for example, represents a special non-numeric value in JavaScript, although it is itself a numeric type.

In JavaScript, there are several special types of numbers:

Infinity//denotes infinity special value

NaN//Special non-numeric value

Number.MAX_VALUE//maximum number that can be represented

Number.min_value//Minimum number to be represented (closest to 0)

Number.NaN//Special non-numeric value

Number.POSITIVE_INFINITY//indicates a special value for positive infinity

Number.negative_infinity//indicates a special value for negative infinity

The above special type, in the operation with TypeOf, the result will be number.


2) for string types, the value returned by TypeOf is String. For example, the value returned by typeof "Jason" is a string.

3) For Boolean types, the value returned by TypeOf is Boolean. For example, the value returned by TypeOf True is Boolean.

4) The value returned for the object, array, and null is object. For example typeof {},typeof [],typeof null returns a value that is an object.


5) for a function type, the value returned is function. For example: The value returned by typeof eval,typeof date is a function.

6) If the operand is undefined (for example, a nonexistent variable, function, or undefined), the undefined will be returned. For example: TypeOf Jason, typeof undefined all return to undefined.

Console.log (typeof a); String

Console.log (typeof B); Number

Console.log (typeof c); Boolean

Console.log (typeof D); Object

Console.log (typeof e); Object

Console.log (typeof F); function

Console.log (typeof 1); Number

Console.log (typeof NaN); Number

Console.log (typeof Number.min_value); Number

Console.log (typeof Infinity); Number

Console.log (typeof "123"); String

Console.log (typeof true); Boolean

Console.log (typeof {}); Object

Console.log (typeof []); Object

Console.log (typeof null); Object

Console.log (typeof eval); function

Console.log (typeof Date); function

Console.log (typeof SSS); Undefined

Console.log (typeof undefined); Undefined


Second, determine whether an object is a data type, or whether a variable is an instance of an object: instanceof

Note: The instanceof must be followed by the object type, and the casing cannot be wrong, and the method is suitable for some conditional selection or branching.

Console.log (d instanceof Array); True

Console.log (e instanceof Date); True

Console.log (f instanceof Function); True


Third, according to the constructor judgment of the object: constructor

Console.log (D.constructor = = = Array)//true

Console.log (E.constructor = = = Date)//true

Console.log (F.constructor = = Function)//true

Note constructor error when inheriting from class

For example:

function A () {};

function B () {};

var aobj = new A ();

Console.log (Aobj.constructor = = = A); True

Console.log (Aobj.constructor = = = B); False

function C () {};

function D () {};

C.prototype = new D (); C Inherits from D

var cobj = new C ();

Console.log (Cobj.constructor = = = C); False

Console.log (Cobj.constructor = = = D); True

The instanceof method does not have this problem, both direct and indirect inheritance of objects are reported true:

Console.log (CObj instanceof C); True

Console.log (CObj instanceof D); True

The problem with solving construtor is usually to let the object's constructor manually point to itself:

Cobj.constructor = C; Assigning your own class to an object's constructor property

Console.log (Cobj.constructor = = = C); True

Console.log (Cobj.constructor = = = D); False The base class does not report true;


Four, general but very cumbersome method: prototype

Console.log (Object.prototype.toString.call (a) = = = ' [Object String] '); True

Console.log (Object.prototype.toString.call (b) = = = ' [Object number] '); True

Console.log (Object.prototype.toString.call (c) = = = ' [Object Boolean] '); True

Console.log (Object.prototype.toString.call (d) = = = ' [Object Array] '); True

Console.log (Object.prototype.toString.call (e) = = = ' [Object Date] '); True

Console.log (Object.prototype.toString.call (f) = = = ' [Object Function] '); True

Note: The case can not be written wrong, more trouble, but wins in general.


Summarize:

Usually use typeof judge can, encounter the situation of the type of object can choose Instanceof or constructor method, simple summary, welcome to add!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.