Adding Properties and Methods to Objects

This section provides a quick description of adding properties and methods to objects only. A tutorial example is provided on adding 1 extra property and 1 extra method to 1 object only.

After an object has been created from the constructor function, additional properties and methods can be added to this object only, using these formats:

   object_name.property_name = initial_value;
   object_name.method_name = function_name;

Here is tutorial example that creates two objects from "Book()", and adds 1 extra property and 1 extra method to object "myBook" only:

<!-- Prototype_Add_Property_and_Method.html
   Copyright (c) 2008 All Rights Reserved.
<title>Adding Additional Properties and Methods to Objects</title>
<script type="text/javascript">

// Defining the constructor function for a new object type
function Book(title, author) {
   this.title = title; = author;

// Defining a function to be used as a method
function getDescription() {
   return "\""+this.title+"\" by ";

   // Creating an object of "Book"
   var myBook = new Book("JavaScript Tutorials", "Herong Yang");
   var ckBook = new Book("JavaScript Cookbook", "Yosef Cohen");

   // Adding additional property and method to one object
   myBook.price = 99.99;
   myBook.getDesc = getDescription;

   // Showing object properties and methods
   showObject(myBook, "myBook");
   showObject(ckBook, "ckBook");

function showObject(object, name) {
   document.writeln("\nShowing object \""+name+"\"");
   document.writeln("   Own Properties and Methods:");
   for (var item in object) {
      document.writeln("      "+item+": "+object[item]);

The output of the tutorial example shows that properties defined in the constructor function are added to both objects. But properties and methods added to object "myBook" are for "myBook" only.

Showing object "myBook"
   Own Properties and Methods:
      title: JavaScript Tutorials
      author: Herong Yang
      price: 99.99
      getDesc: function getDescription() {
    return "\"" + this.title + "\" by " +;

Showing object "ckBook"
   Own Properties and Methods:
      title: JavaScript Cookbook
      author: Yosef Cohen

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

 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

 AJAX (Asynchronous JavaScript And XML)


 Full Version in PDF/EPUB