We all know that Javascript typeof can get the type of a variable, but the TypeOf return value is only six "number," "String," "Boolean," "Object," "function," and "undefined."
In fact, Javascript also has a number of special categories such as Array, Date. Why is it not possible to return in typeof?
The original Javascript was to classify the Array Date object into the object class. We can only judge the exact category of object by instanceof.
Here is a simple example to illustrate the use of instanceof
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 ();
Response.Write (Objtest (obj));