Differences between javascript instanceof and typeof

Source: Internet
Author: User

Why is the result false?
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var aColors = ["red", "green", "blue"];
Alert (typeof aColors [0]); // output "string"
Alert (aColors [0] instanceof String); // output "false ";
</Script>

You need to distinguish string from String.
AColors [0] is a string value type. Of course it is not a String instance. Refer to the following code
Var aColors = ["red", "green", "blue"];
AColors [0] = new String ("1 ")
Alert (typeof aColors [0]); // output "Object"
Alert (aColors [0] instanceof String); // output "true ";

For more information, see the following article:

Instanceof Operator
Returns a Boolean value indicating whether the object is an instance of a specific class.
Result = object instanceof class
Parameters
Result
Required. Any variable.
Object
Required. Any object expression.
Class
Required. Any defined object class.
Description
If an object is an instance of class, the instanceof operator returns true. If the object is not an instance of the specified class, or the object is null, false is returned.
Example
The following example illustrates the usage of the instanceof operator.
Copy codeThe Code is as follows:
Function objTest (obj ){
Var I, t, s = ""; // create a variable.
T = new Array (); // create an Array.
T ["Date"] = Date; // fill in the array.
T ["Object"] = Object;
T ["Array"] = Array;
For (I in t)
{
If (obj instanceof t [I]) // check the obj class.
{
S + = "obj is an instance of" + I + "\ n ";
}
Else
{
S + = "obj is not an instance of" + I + "\ n ";
}
}
Return (s); // return a string.
}
Var obj = new Date ();
Document. write (objTest (obj ));

Both instanceof and typeof can be used to determine whether a variable is null or of any type.
Typeof is used to obtain the type of a variable. Generally, typeof can only return the following results: number, boolean, string, function, object, undefined. We can use typeof to obtain whether a variable exists, such as if (typeof! = "Undefined") {}, instead of using if (a) because if a does not exist (not declared), an error occurs. For Array, null and other special objects use typeof to return all objects, which is the limitation of typeof.
If you want to obtain whether an object is an array or determine whether a variable is an instance of an object, you must use instanceof. Instanceof is an instance used to determine whether a variable is an Object, such as var a = new Array (); alert (a instanceof Array); true is returned, and alert (a instanceof Object) true is also returned because Array is a subclass of the object. For example, function test () {}; var a = new test (); alert (a instanceof test) returns true.
When talking about instanceof, We need to insert another question: function arguments. We may all think that arguments is an Array. However, if we use instaceof to test, we will find that arguments is not an Array object, although it looks very similar.
In addition:
Test var a = new Array (); if (a instanceof Object) alert ('y'); else alert ('n ');
Get 'y'
However, if (window instanceof Object) alert ('y'); else alert ('n ');
'N'
Therefore, the instanceof object here refers to the object in js syntax, not the dom model object.
There are some differences when typeof is used.
Alert (typeof (window) will get the object
When you are young, you need to talk less nonsense and do more things.

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.