This section provides a quick description of the 'instanceof' operator. A tutorial example is provided on how to use the 'instanceof' operator to determine what is the type of an object, and its parent types.
So far, we have played with several types of objects, like Object, Array, Function, and Book (my own object type).
And we know how to create objects with these type by using their constructor functions.
But we are given an object, how can we test its object type? JavaScript offers the "instanceof" operator to help us
with this expression
object_name instanceof constructor_name;
This "instanceof" expression returns a Boolean value indicating whether or not the specified object
is an instance of the specified type (the constructor name represents the object type name).
The tutorial examples below uses the "instanceof" operator to test 3 objects used in my previous example:
<html>
<!-- Prototype_instanceof_Operator.html
Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head>
<title>The "instanceof" Operator - Determining Object Typs</title>
</head>
<body>
<pre>
<script type="text/javascript">
// Defining object type constructor function
function Book(title, author) {
this.title = title;
this.author = author;
}
// Adding properties and methods to the "prototype"
Book.prototype.price = 9.99;
Book.prototype.getDesc = getDescription;
// Defining a function to be used as a method
function getDescription() {
return "\""+this.title+"\" by "+this.author;
}
// Creating an object of "Book"
var myBook = new Book("JavaScript Tutorials", "Herong Yang");
// Showing the object, constructor, and prototype
showObject(myBook, "myBook");
showObject(Book, "Book");
showObject(Book.prototype, "Book.prototype");
showObject(Object, "Object");
showObject(Object.prototype, "Object.prototype");
function showObject(object, name) {
document.writeln("\nShowing object \""+name+"\"");
// Using "instanceof" to determine object type
document.writeln(" Instance of Object: "
+(object instanceof Object));
document.writeln(" Instance of Function: "
+(object instanceof Function));
document.writeln(" Instance of Book: "
+(object instanceof Book));
}
</script>
</pre>
</body>
</html>
Here is the output of this tutorial example:
Showing object "myBook"
Instance of Object: true
Instance of Function: false
Instance of Book: true
Showing object "Book"
Instance of Object: true
Instance of Function: true
Instance of Book: false
Showing object "Book.prototype"
Instance of Object: true
Instance of Function: false
Instance of Book: false
Showing object "Object"
Instance of Object: true
Instance of Function: true
Instance of Book: false
Showing object "Object.prototype"
Instance of Object: false
Instance of Function: false
Instance of Book: false
Several interesting notes on the output
"myBook" is an object created with "Book()". So it is an instance of "Book".
But "myBook" is also an instance of "Object", because "Book" is a subtype of "Object".
All used defined object types, by default, are subtypes of the base "Object" object type.
"Book" is a function. So it is an instance of "Function".
But "Book" is also an instance of "Object", because "Function" is a subtype of "Object".
"Book.prototype" is an instance of the base object type: "Object".
"Object" is a function. So it is an instance of both object types: "Function" and "Object".
However "Object.prototype" is an object. So it should be an instance of "Object".
But the output says no. May be JavaScript has assigned "Object.prototype" with an internal object type.