In JavaScript, The instanceof operator returns a Boolean value indicating whether the object is an instance of a specific class.
Usage:
Result = object instanceof class
Result is required. Any variable.
Object is required. Any object expression.
Class is 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.
Instanceof operator in JavaScript
The following example illustrates the usage of the instanceof operator.
Copy codeThe 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)
{
If (obj instanceof t [I]) // check the obj class.
{
S + = "obj is an instance of" + I + "/n ";
}
Else
{
S + = "obj is not an instance of" + I + "/n ";
}
}
Return (s); // return a string.
}
Var obj = new Date ();
Response. write (objTest (obj ));