JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.10

Defining Object Constructor Functions

This section provides a quick description on defining object constructor functions. A tutorial example is provided on creating a 'Book' constructor with 3 properties and 1 method.

The first thing to learn about prototype-based programming style in JavaScript is the constructor function. When writing a constructor function, we need to remember:

  • The constructor function name will be the object type name. So select a good name for the constructor function.
  • In the constructor function, "this" keyword can be used to represent the object to be constructed.
  • Properties should be added in the constructor function with assignment expressions as "this.property_name = initial_value".
  • Constructor function should take some parameters to initialize properties.
  • Methods should be added in the constructor function with assignment expressions as "this.method_name = function_name", where "function_name" refers to an existing function.
  • Don't use any "return" statement in the constructor function. The return value will be ignored when it is called as an object constructor with the "new" operator.

Here is a tutorial example that defines a constructor function called "Book(...)", which defines 3 properties and 1 method:

<html>
<!-- Prototype_Constructor_Function.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head>
<title>Defining Object Constructor Functions</title>
</head>
<body>
<pre>
<script type="text/javascript">

// Defining the constructor function for a new object type
function Book(title, author) {
   document.writeln("\nConstructing a \"Book\" object...");
   this.title = title;
   this.author = author;
   this.price = undefined;
   this.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");
   document.writeln("\nChecking the \"Book\" object");
   document.writeln("   Type: "+(typeof myBook));
   document.writeln("   Instance of \"Book\": "
      +(myBook instanceof Book));

   // Testing properties and methods
   document.writeln("   myBook.title: "+myBook.title);
   document.writeln("   myBook.author: "+myBook.author);
   document.writeln("   myBook.price: "+myBook.price);
   document.writeln("   myBook.getDesc(): "+myBook.getDesc());

   // Trying to call a constructor function as a normal function
   Book("JavaScript Cookbook", "Yosef Cohen");
</script>
</pre>
</body>
</html>

The output of the tutorial example is printed below:

Constructing a "Book" object...

Checking the "Book" object
   Type: object
   Instance of "Book": true
   myBook.title: JavaScript Tutorials
   myBook.author: Herong Yang
   myBook.price: undefined
   myBook.getDesc(): "JavaScript Tutorials" by Herong Yang

Constructing a "Book" object...

Notice that the last test statement proves that a constructor function is just a normal function, and you can call it directly. But calling a constructor function directly will not produce any result.

As a quick summary:

  • Constructor equals to function. A constructor is a function. And a function is a constructor.
  • A constructor can be used to create a new object with the "new" operator.
  • Any constructor can be called to execute as a function.
  • Any function can be used to create objects as a constructor.
  • Sections in This Chapter

    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

    Dr. Herong Yang, updated in 2008
    Defining Object Constructor Functions