JavaScript instanceof and TypeOf usage Description _ Basics

Source: Internet
Author: User
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 whether a variable exists, as

if (typeof a!= "undefined") {}, do not use if (a) because if a does not exist (not declared) will be an error, for Array,null and other special objects use TypeOf all return object, this 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.

The instanceof operator in JavaScript is to return a Boolean value that indicates whether an object is an instance of a particular class.
How to use:
Result = Object Instanceof class
Where result is a required option. Any variable.
Object is a required option. An arbitrary object expression.
Class is a 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.

Instanceof operator in JavaScript
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 ();//Create an array.
t["date" = date;//fill 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 ' not ' instance of + i + ' \ n ';
}
}
return (s);//Returns a string.
}

var obj = new Date ();
Response.Write (objtest (obj));
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.