HTML and CSS Reference
In-Depth Information
EXAMPLE 8.15 ( CONTINUED )
8
if( tale.hasOwnProperty("author") ){
document.write("The object has an author property.<br />");
}
9
if( Book.prototype.isPrototypeOf(tale) ){
document.write("Book is a prototype of object tale.<br />");
}
10
if( tale instanceof Book ){
document.write("tale is an instance of Book.<br />");
}
11
if( tale instanceof Object ){
document.write("tale is an instance of Object.<br />");
}
</script>
</big>
</body>
</html>
EXPLANATION
1 The constructor function called Book defines the properties and methods for a
Book class.
2 The Book class starts with an empty prototype object. Now we assign it a property.
The publisher property and its value are available to all Book objects.
3 By assigning a property to the Object object's prototype, all instances of the Book
class (or any class) will have access to the category property.
4 A ew i t e f t e k l s is t , a f e to n j t ll d tale .
5
The value of the Object object's prototype property, category , is displayed.
6
The constructor property is inherited from the Object object and can be used with
any of its descendants. This line tests if this object has a constructor. It does, so
the block is entered.
7
The value assigned to the constructor property is the constructor function and the
names of all of its arguments and properties.
8
The hasOwnProperty() method returns true if the property (its argument) belongs
to the object being tested; in this case the object is tale and the method is checking
to see if the author property belongs to it and is not inherited from the prototype
chain.
9
Next we test to see if the tale object is inheriting from the Book class; that is, Book
is a prototype for the tale class.
10
The instanceof operator returns true if the specified object, tale, is of the specified
object type; that is, tale is a Book type.
11
The instanceof operator returns true if the specified object, tale , is of the specified
object type; that is, tale is also an Object type. The output is shown in Figure 8.18.
 
Search WWH ::




Custom Search