The difference _javascript skills of JavaScript instanceof,typeof

Source: Internet
Author: User
Why would the result be false?
Copy Code code 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 want to distinguish string from string.
Acolors[0] is a string value type and certainly not an instance of string. 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, refer to the following article:

instanceof operator
Returns a Boolean value that indicates whether an object is an instance of a particular class.
Result = Object Instanceof class
Parameters
Result
Required option. Any variable.
Object
Required option. An arbitrary object expression.
Class
Required option. Any defined object class.
Description
If object is an instance of class, the instanceof operator returns TRUE. Returns False if object is not an instance of the specified class, or if object is null.
Example
The following example illustrates the use of the instanceof operator.
Copy Code code as follows:

function Objtest (obj) {
var i, T, s = ""; Create a variable.
t = new Array (); Creates an array.
t["date" = date; Fills the array.
T["Object" = object;
t["Array" = array;
for (i in T)
{
if (obj instanceof t[i])//Check the class of obj.
{
s + = "obj is an instance of" + i + "\ n";
}
Else
{
s + = "obj is isn't an instance of" + i + "\ n";
}
}
return (s); Returns a string.
}
var obj = new Date ();
document.write (Objtest (obj));

Both instanceof and typeof can be used to determine whether a variable is empty or what type of variable it is.
typeof is used to get the type of a variable, typeof generally can only return the following results: number,boolean,string,function,object,undefined. We can use TypeOf to get the existence of a variable, such as if (typeof a!= "undefined") {}, and not to use if (a) because if a does not exist (not declared) there will be an error, for special objects such as array,null use typeof All return object, which is the limitation of TypeOf.
If we want to get an object to be an array, or to determine whether a variable is an instance of an object, choose to use instanceof. Instanceof is used to determine whether a variable is an instance of an object, such as the Var a=new array (); alert (a instanceof array); Returns true while alert (a Instanceof object) Also returns true, because array is a subclass of object. Again, the function test () {};var a=new test (), alert (a instanceof test) returns True.
When it comes to instanceof, we have to insert one more question, which is the arguments of the function, and we all probably think that arguments is an array, But if you use instaceof to test, you will find that arguments is not an array object, although it looks like it.
Other than that:
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 ');
Get ' N '
So, the object of the instanceof test here refers to the object in the JS syntax, not the DOM model object.
There are some differences between using typeof
Alert (window) gets object typeof
When you are young, start with less nonsense and do more work.
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.