Differences between JavaScript instanceof and typeof

Source: Internet
Author: User

Why is the result false? CopyCode The 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 followingArticle:

instanceof operator
Returns a Boolean value indicating whether the object is an instance of a specific class.
result = Object instanceof class
parameter
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 Code the 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)
{< br> If (OBJ instanceof T [I]) // check the OBJ class.
{< br> S + = "obj is an instance of" + I + "\ n ";
}< br> else
{< br> S + = "obj is not an instance of" + I + "\ n ";
}< BR >}< br> return (s); // return a string.
}< br> 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, and 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 one more question, that is, the arguments of function. We may all think that arguments is an array, however, if you use instaceof for testing, you 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'
but if (window instanceof object) Alert ('y'); else alert ('n ');
'n'
therefore, the instanceof test object here refers to the object in JS syntax, not the DOM model object.
there are some differences between using typeof
alert (typeof (window) will get the object
when you are young, you should first do more things without talking nonsense.

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.