How to infer the type of JS, first 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 inference method: typeof
typeof is a unary operator that returns the result that is always a string, returns different results for different operands, and typeof can infer the type of the function, which is more convenient when inferring objects of type object.
Console.log (typeof a = = "string"); True
Console.log (typeof a = = String); False
Detailed rules such as the following:
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 a very regular number type, the result is also the numbers returned. 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) Assuming that 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 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, infer whether an object is a data type, or whether a variable is an instance of an object: instanceof
Note: instanceof must be followed by object type, and uppercase and lowercase cannot be wrong, this 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
Constructor inference based on 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
Like what:
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 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: Uppercase and lowercase can not be written wrong, more troublesome, but wins in general.
Summarize:
Usually use typeof inference to be able to encounter the situation of the type of object can be predicted instanceof or constructor method, simple summary, welcome to add!
Infer the type in JS: Typeof/instanceof/constructor/prototype