JavaScript typeof operator

Source: Internet
Author: User
Tags date array object definition object object string variable tostring

typeof operator
The typeof operator (together with instanceof) may be the biggest design flaw in JavaScript,
Because it is almost impossible to get the desired results from them.
Although Instanceof still has a few application scenarios, typeof has only one practical application: The actual application is used to detect whether an object has been defined or has been assigned a value.
This application is not used to check the type of object.
Note: Because typeof can also be invoked like the syntax of a function, such as typeof (obj), this is a function call.
The two parentheses are used to compute the value of an expression, which is the operand of the typeof operator.
In fact, there is no function named typeof.
JavaScript Type Table
Value Class Type
-------------------------------------
"Foo" string string
New String ("foo") string object
1.2 Number number
New Number (1.2) Number Object
True Boolean Boolean
New Boolean (True) Boolean object
New Date () Date Object
New error () Error object
[1,2,3] Array Object
New Array (1, 2, 3) Array object
New function ("") function function
/abc/g RegExp Object (function in NITRO/V8)
New RegExp ("Meow") RegExp object (function in NITRO/V8)
{} Object Object
New Object () object
In the table above, Type a column represents the result of the operation of the typeof operator. As you can see, this value returns "Object" in most cases.
Class a column represents the value of an object's internal property [[Class]].
JavaScript standard document definition: [[Class]] value may only be one of the following strings:
Arguments, Array, Boolean, Date, Error,
Function, JSON, Math, number, Object, REGEXP, String.
To get the [[Class]] of the object, we need to use the method toString defined on the Object.prototype.
Class definition of the object
The JavaScript standard document only gives a way to get [[Class]] values, and that is to use Object.prototype.toString.
function is (type, obj) {
var clas = Object.prototype.toString.call (obj). Slice (8,-1);
return obj!== undefined && obj!== null && clas = = type;
}
Is (' String ', ' test '); True
Is (' String ', new string (' Test ')); True
In the example above, the Object.prototype.toString method is called, this is set to obtain an object that requires a [[Class]] value.
Object.prototype.toString returns a standard format string, so the previous example can intercept the string at the specified position by slice, as follows:
Object.prototype.toString.call ([])//"[Object Array]"
Object.prototype.toString.call ({})//"[Object Object]"
Object.prototype.toString.call (2)//"[Object number]"
ES5 tip: In ECMAScript 5, for convenience, call the Object.prototype.toString method on null and undefined,
The return value is changed from Object to Null and Undefined.
Translator Note: This change can be seen from IE8 and Firefox 4, as follows:
IE8
Object.prototype.toString.call (NULL)//"[Object Object]"
Object.prototype.toString.call (undefined)//"[Object Object]"
Firefox 4
Object.prototype.toString.call (NULL)//"[Object NULL]"
Object.prototype.toString.call (undefined)//"[Object undefined]"
Test to define a variable
typeof foo!== ' undefined '
The code above detects whether Foo has been defined, and if it is not defined, it can cause referenceerror exceptions.
This is the only useful place for TypeOf.
Conclusion
In order to detect the type of an object, it is strongly recommended to use Object.prototype.toString method;
Because this is the only way to be relied on. As shown in the table above, some of the return values for typeof are not defined in the standard document.
Therefore, different engine implementations may be different.
Unless you want to detect whether a variable has been defined, we should try to avoid using the typeof operator.



Related Article

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.