"instanceof" Operator - Determining Object Type
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.
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:
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
Table of Contents