1. Function:
The typeof operator returns a String that represents the data type of an expression.
possible strings are: "Number", "string", "Boolean", "Object", "function", and "undefined".
2, the common return value explanation
An expression |
return value |
typeof undefined |
' Undefined ' |
typeof null |
' Object ' |
typeof true |
' Boolean ' |
typeof 123 |
' Number ' |
typeof "ABC" |
' String ' |
typeof function () {} |
' function ' |
typeof {} |
' Object ' |
typeof [] |
' Object ' |
typeof Unknownvariable |
' Undefined ' |
Note: The type return value is a string and is all lowercase.
3. General application:
1. Check to see if a variable exists and has a value.
typeof will return "undefined" in two cases: when a variable is not declared, and when the value of a variable is undefined. For example:
> typeof undeclaredvariable = = "undefined" true
> var declaredvariable;
> typeof declaredvariable ' undefined '
> typeof undefined ' undefined '
There are other ways to detect whether a value is undefined:
> var value = undefined;
> value = = undefined true
But this method throws an exception if it is used on an undeclared variable, because only typeof can detect undeclared variables without error:
> undeclaredvariable = = undefined referenceerror:undeclaredvariable = Not defined
Note: Uninitialized variables, no formal parameters that are passed in, no properties that do not exist, do not appear above the problem because they are always accessible and the values are always undefined:
> var declaredvariable;
> declaredvariable = = undefined true
> (function (x) {return x = = undefined} ()) True
> ({}). Foo = = undefined true
Note: Therefore, if you want to detect the existence of a global variable that may not be declared, you can also use the IF (window.maybeundeclaredvariable) {}
question: typeof is very complicated in accomplishing such a task.
Solution: This is not a very common operation, so some people feel that there is no need to find a better solution. But perhaps someone will put forward a special operator:
> Defined undeclaredvariable False
> var declaredvariable;
> Defined declaredvariable False
Alternatively, someone might also need an operator that detects whether a variable is declared:
> Declared undeclaredvariable False
> var declaredvariable;
> Declared declaredvariable True
Translator Note: In Perl, the defined operator above is equivalent to defined (), and the declared operator above is equivalent to exists (),
2. Judge a value not equal to undefined and not equal to NULL
Question: If you want to detect whether a value has been defined (the value is not undefined or null), then you encounter the TypeOf's most famous bizarre expression (considered a bug): typeof null Returns "Object":
> typeof null ' object '
Note: This can only be said to be the original JavaScript implementation of the bug, and now the standard is such a specification. V8 has fixed and implemented typeof null = = "NULL", but the final proof is not OK. Http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null
Solution: Do not use typeof to do this task, in the following way to replace: But the variable x must be declared in advance, otherwise it will be an error.
function isdefined (x) {return x!== null && x!== undefined;}
Another possibility is to introduce a "default value operator" that, in the case of myvalue undefined, returns the following expression defaultvalue:
myvalue | | DefaultValue
The expression above is equivalent to:
(myvalue!== undefined && myvalue!== null)? Myvalue:defaultvalue
This is actually a simplification of the following statement:
myvalue = MyValue | | DefaultValue
3. Distinguish between object values and original values
The following function is used to detect whether X is an object value:
function IsObject (x) {return (typeof x = = "function" | | (typeof x = = "Object" && x!== null)); }
Problem: The above detection is more complex because TypeOf regards functions and objects as different types, and typeof null returns "Object".
Solution: The following methods are also frequently used to detect object values:
function IsObject2 (x) {return x = = = = Object (x);}
Warning: You may think that you can use Instanceof object to detect this, but instanceof is using a prototype of an object to determine the instance relationship, so what about the object without a prototype:
> var obj = object.create (null);
> object.getprototypeof (obj) null
Obj is indeed an object, but it is not an instance of any value:
> typeof obj ' object '
> obj instanceof Object false
In practice, you may rarely encounter such an object, but it does exist and has its purpose.
Translator Note: Object.prototype is a default, no prototype object
>object.getprototypeof (Object.prototype) null
>typeof object.prototype ' Object '
>object.prototype instanceof Object False
4. What is the type of the original value?
typeof is the best way to view the type of a raw value.
> typeof "abc" ' String '
> typeof undefined ' undefined '
Question: You must know the strange typeof of NULL.
> typeof null//Be careful! ' Object '
Workaround: The following function can fix this problem (for this use case only).
function Getprimitivetypename (x) {
var typeName = typeof x;
Switch (typeName) {
Case ' undefined ': Case ' Boolean ': Case ' number ': Case ' string ': return typeName;
Case "Object": if (x = = null) {return ' null ';}
Default://Previous judgment did not pass throw new TypeError ("parameter is not an original value:" +x);
} }
A better solution: Implement a function gettypename (), in addition to the type that can return the original value, you can return the internal [[Class]] property of the object value. Here's how to implement this function (translator: $ in jquery). Type is the implementation of this)
5. Whether a value is a function
typeof can be used to detect whether a value is a function.
> typeof function () {} ' function '
> typeof Object.prototype.toString ' function '
In principle, the instanceof function can also be tested for this requirement. At first glance, it looks more elegant. However, browsers have a quirk: each frame and window has its own global variables. So if you pass objects in one frame to another, Instanceof will not work properly because the two frameworks have different constructors. That's why the Array.isarray () method is ECMAScript5. If there is one that can cross the frame, It would be nice to check whether an object is an instance of a given constructor. The above Gettypename () is an available workaround, but there may be a more fundamental solution.
6. Overview
As mentioned below, this should be the most pressing need in current JavaScript to replace some of the functional features of TypeOf's current responsibilities:
IsDefined () (for example, object.isdefined ()): Can be used as a function or as an operator
IsObject ()
Gettypename ()
A mechanism that can cross a frame to detect whether an object is an instance of a specified constructor
It may not be necessary to have your own operator to check whether a variable has been declared as such.
The above this JS in the judgment variable type function typeof usage summary is small series share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.