What is the use of开发者_如何学编程 the typeof
function?
typeof
is actually an operator, not a function, it returns you the type of the supplied operand, and returns you always[1] a string value, that can be:
"undefined"
"object"
"boolean"
"number"
"string"
"function"
It can look like a function, because it can be applied to parenthesised expressions, but the parentheses are not mandatory, you can use it without them:
typeof expression
Is worth mentioning that you can reference even an identifier that is not declared (an unresolvable reference, e.g. a variable never declared) and it will not throw a ReferenceError
, it will simply produce "undefined"
, for example:
if (typeof imUndeclared == 'undefined') {
// Ok...
}
// Vs.
if (imUndeclared === undefined) { // ReferenceError!
}
A case that can cause confusion to newcomers to the language is that the typeof
operator returns "object"
for a null
value, you should keep that in mind...
Also if you want to distinguish between Array
objects, RegExp
objects, Date
objects, etc, it isn't too useful, but there are other ways to accomplish it.
[1] With the infamous exception of JScript (IE) :), which can actually return "unknown"
!!! for some host objects, e.g. typeof new ActiveXObject("Msxml2.XMLHTTP").abort; // "unknown"
The typeof
Operator
typeof
is a an unary operator (and NOT a function), defined by the ECMAScript standard and therefore implemented by most existing JavaScript implementations; though historically speaking, it was part of JavaScript since its version 1.1, before its ownership was handed over to ECMA International.
It is used to introspect the type of a variable by querying it (typeof expression
) and checking the return value, which is a string
object indicating the type with lossy precision (see below).
Usage
As per the ECMAScript 5 Standard (ECMA-262, p. 71), it will return the following string values when tested with these given parameters of the following types:
Undefined
--> "undefined"Null
--> "object"Boolean
--> "boolean"Number
--> "number"String
--> "string"Object
--> it depends:- native and does not implement
Call
--> "object" - native and does implement
Call
--> "function" - host and does not implement
Call
--> Implementation-defined except may not be "undefined", "boolean", "number", or "string"
- native and does not implement
For most implementations, typeof
will also return:
Array
--> "object"Function
--> "object" (see aboveObject
's 2nd and 3rd case...)
(Note that types are capitalized, whereas their string identifiers as returned by typeof
are not)
Caveats
- As mentioned above, the type detection with
typeof
is lossy. typeof
, as of ECMAScript 5, still doesn't supportArray
(which is identified as "object" in most cases. (see Douglas Crockford's Remedial JavaScript)- Detection of
Function
andArray
varies.
Additional References
- Wikipedia's ECMAScript Syntax page will also provide usage examples of
typeof
. - Mozilla's Developer Center's JavaScript Reference for typeof
The typeof operator returns a string indicating the type of the operand.
Some people have suggested nice typeOf functions. One that I recently read up on and tested thoroughly was from Chris' blog found here. What I like about his solution is that it will always work for any native JavaScript object. If you supply just the object, it will tell you the type of it as expected:
alert(typeOf([1,2,3])); // Array
alert(typeOf(123)); // Number
alert(typeOf(/a_reg_exp/gi)); // RegExp
alert(typeOf(null)); // null
alert(typeOf(undefined)); // undefined
If the second parameter is a string that may identify the type, the equality between type of the object and the specified type name will be returned as a boolean:
alert(typeOf([1,2,3], "Array")); // true
alert(typeOf(123, "String")); // false
alert(typeOf(/a_reg_exp/gi, "RegExp")); // true
alert(typeOf(null, "undefined")); // false
alert(typeOf(undefined, "undefined")); // true
To check variable type(whether it is array, object, function etc.).
精彩评论