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));