Home  >  Article  >  Web Front-end  >  Summary of usage of instanceof operator in JavaScript_javascript tips

Summary of usage of instanceof operator in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 17:14:111824browse

The instanceof operator in JavaScript returns a Boolean value, indicating whether the object is an instance of a specific class.

Usage:
result = object instanceof class
where result is a required option. Any variable.
object is required. Any object expression.
class is required. Any defined object class.

Explanation
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 usage of instanceof operator.

Copy code The code is as follows:

function objTest(obj){
var i, t, s = ""; // Create a variable.
t = new Array(); // Create an array.
t["Date"] = Date; // Fill the array.
t["Object"] = Object;
t["Array"] = Array;
for (i in t)
{
if (obj instanceof t[i]) / / Check the class of obj.
                                                                                                                                                                   " i "/ n";
}
}
return(s); // Return a string.
}

var obj = new Date();
response.write(objTest(obj));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn