JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
"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.
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 HerongYang.com. All Rights Reserved. --> <head> <title>The "instanceof" Operator - Determining Object Types</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
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
►Defining Your Own Object Types
Prototype-Dased Object-Oriented Programming Style
Prototype-Based Programming Features in JavaScript
Defining Object Constructor Functions
Adding Properties and Methods to Objects
"for ... in" and "for each ... in" Statements
"prototype" Property of the Constructor Function Object
►"instanceof" Operator - Determining Object Type
"typeof" Operator and Data Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
Introduction to Built-in Object Types
W3C's Document Object Model (DOM) Specifications