Using "hasOwnProperty()" Method to Test Inherited Properties

This section provides a quick description of the built-in default method called 'hasOwnProperty()'. A tutorial example is provided to use 'hasOwnProperty()' method to test which property is local to this object and which is inherited.

From the previous section, we learned that "Object.prototype" has a method called "hasOwnProperty()", which will be inherited by all objects in JavaScript.

From any object, you can use the "hasOwnProperty(prop)" method to test whether the specified property is local to this object.

With this nice tool, I want to revisit my "Book" object example to check different properties at different inheritance level with this tutorial example:

<html>
<!-- Inheritance_hasOwnProperty_Method.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
-->
<head>
<title>Using "hasOwnProperty" Method</title>
</head>
<body>
<pre>
<script type="text/javascript">

// Setting up my constructor, prototype and object
function Book(title, author) {
   this.title = title;
   this.author = author;
}
   Book.prototype.price = 9.99;
   Object.prototype.copyright = "herongyang.com";
   var myBook = new Book("JavaScript Tutorials", "Herong Yang");

   // Dumping built-in properties at the base prototype level
   document.writeln("\nObject.prototype's built-in properties:");
   dumpProperty(Object.prototype, "constructor");
   dumpProperty(Object.prototype, "hasOwnProperty");
   dumpProperty(Object.prototype, "isPrototypeOf");
   dumpProperty(Object.prototype, "toString");
   dumpProperty(Object.prototype, "valueOf");

   // Dumping built-in properties at the my prototype level
   document.writeln("\nBook.prototype's built-in properties:");
   dumpProperty(Book.prototype, "constructor");
   dumpProperty(Book.prototype, "hasOwnProperty");
   dumpProperty(Book.prototype, "isPrototypeOf");
   dumpProperty(Book.prototype, "toString");
   dumpProperty(Book.prototype, "valueOf");

   // Dumping built-in properties at the object level
   document.writeln("\nmyBook's built-in properties:");
   dumpProperty(myBook, "constructor");
   dumpProperty(myBook, "hasOwnProperty");
   dumpProperty(myBook, "isPrototypeOf");
   dumpProperty(myBook, "toString");
   dumpProperty(myBook, "valueOf");

function dumpProperty(object, property) {
   var inheritance; 
   if (object.hasOwnProperty(property)) 
      inheritance = "Local";
   else 
      inheritance = "Inherited";
   document.writeln("   "+property+": "+inheritance+": "
      +object[property]);
}
</script>
</pre>
</body>
</html>

Here is the output this tutorial example:

Object.prototype's built-in properties:
   constructor: Local: function Object() {
    [native code]
}
   hasOwnProperty: Local: function hasOwnProperty() {
    [native code]
}
   isPrototypeOf: Local: function isPrototypeOf() {
    [native code]
}
   toString: Local: function toString() {
    [native code]
}
   valueOf: Local: function valueOf() {
    [native code]
}

Book.prototype's built-in properties:
   constructor: Local: function Book(title, author) {
    this.title = title;
    this.author = author;
}
   hasOwnProperty: Inherited: function hasOwnProperty() {
    [native code]
}
   ...

myBook's built-in properties:
   constructor: Inherited: function Book(title, author) {
    this.title = title;
    this.author = author;
}
   ...

Notice that:

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

Inheritance of Properties and Methods through the Prototype Object Chain

 Inheriting from Constructor's Prototype Object

 Changing the Constructor's Prototype Object

 Inheriting Properties from Two Level Prototype Objects

 Built-in Properties and Methods in "Object.prototype"

Using "hasOwnProperty()" Method to Test Inherited Properties

 Setting the "constructor" Property in the "prototype" Object

 Adding Local Methods at the Object Level

 Adding Inherited Methods at the Prototype Level

 Building Multiple Levels of Prototype Objects

 Prototype Object Chain Summary

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version