Book Image

Object-Oriented JavaScript

By : Stoyan Stefanov, Stoyan STEFANOV
Book Image

Object-Oriented JavaScript

By: Stoyan Stefanov, Stoyan STEFANOV

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
About the Author
About the Reviewers
Built-in Functions
Regular Expressions


This book explores JavaScript for what it is: a highly expressive and flexible prototype-based object-oriented programming language. Once dismissed as a toy for designers to make things such as rollover buttons, today this interesting and unique language is back, stronger than ever. Today's Web 2.0 world of AJAX, fat-client programming, desktop-like rich Internet applications, drag-and-drop maps and webmail clients, rely heavily on JavaScript to provide a highly interactive user experience. And if we never had the chance to properly explore JavaScript before, now is the time to sit down and (re-)learn it.

This book doesn't assume any prior knowledge of JavaScript and works from the ground up to give you a thorough understanding of the language.

What This Book Covers

Chapter 1 talks briefly about the history, present, and future of JavaScript, and then moves on to explore the basics of object-oriented programming (OOP) in general. You then learn how to set up your training environment (Firebug) in order to dive into the language on your own, using the book examples as a base.

Chapter 2 discuses the language basics: variables, data types, arrays, loops, and conditionals.

Chapter 3 covers functions. JavaScript has many uses for functions and here you learn to master them all. You also learn about the scope of variables and JavaScript's built-in functions. An interesting, but often misunderstood feature of the language—closures—is demystified at the end of the chapter.

Chapter 4 introduces objects: how to work with properties and methods, and the various ways to create your objects. There's also an overview of the built-in objects in JavaScript, such as Math and Date (just an overview, Appendix C has all the details).

Chapter 5 is dedicated to the all-important concept of prototypes in JavaScript.

Chapter 6 expands your "thinking in JavaScript" horizon, discussing a dozen ways to implement inheritance in JavaScript.

Chapter 7 is the browser chapter. In this chapter, you learn about BOM (Browser Object Model), DOM (W3C's Document Object Model), browser events, and AJAX.

Chapter 8 dives into various unique JavaScript coding patterns, as well as several language-independent design patterns, translated to JavaScript from the Book of Four, the most influential work of software design patterns.

Appendix A lists the reserved words in JavaScript.

Appendix B is a reference to the built-in JavaScript functions, together with sample uses.

Appendix C is a reference that provides detail and provides examples of the use of every method and property of every built-in object in JavaScript.

Appendix D is a regular expressions pattern reference.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: " The key/value pairs are divided by colons, in the format key: value ."

A block of code will be set as follows:

var book = {
  name: 'Catch-22',
  published: 1961,
  author: {
    firstname: 'Joseph',
    lastname: 'Heller'

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be shown in bold:

function TwoDShape(){}
// take care of inheritance
TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book— what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an email to , and mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.


Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any mistakes, report them by visiting, selecting your book, clicking on the let us know link, and entering the details of the error. Once your comments have been verified, your submission will be accepted and added to the list of existing errata for the book. Any existing errata can be viewed by selecting your book from


If you are having a problem with some aspect of the book you can contact us at , and we will do our best to address it.

I'd like to dedicate this book to my wife Eva and my daughters Zlatina and Nathalie. Thank you for your patience, support, and encouragement.

To my reviewers who volunteered their time reviewing drafts of this book and whom I deeply respect and look up to: a big thank you for your invaluable input.