Object-Oriented JavaScript

Object-Oriented JavaScript
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 40%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Learn to think in JavaScript, the language of the web browser
  • Object-oriented programming made accessible and understandable to web developers
  • Do it yourself: experiment with examples that can be used in your own scripts
  • Write better, more maintainable JavaScript code

Book Details

Language : English
Paperback : 356 pages [ 235mm x 191mm ]
Release Date : July 2008
ISBN : 1847194141
ISBN 13 : 9781847194145
Author(s) : Stoyan Stefanov
Topics and Technologies : All Books, Web Development, AJAX, Open Source, Web Development

Table of Contents

Chapter 1: Introduction
Chapter 2: Primitive Data Types, Arrays, Loops, and Conditions
Chapter 3: Functions
Chapter 4: Objects
Chapter 5: Prototype
Chapter 6: Inheritance
Chapter 7: The Browser Environment
Chapter 8: Coding and Design Patterns
Appendix A: Reserved Words
Appendix B: Built-in Functions
Appendix C: Built-in Objects
Appendix D: Regular Expressions
  • Chapter 1: Introduction
    • A Bit of History
    • The Winds of Change
    • The Present
    • The Future
    • Object-Oriented Programming
      • Objects
      • Classes
      • Encapsulation
      • Aggregation
      • Inheritance
      • Polymorphism
    • OOP Summary
    • Setting up Your Training Environment
      • Getting the Tools You Need
    • Using the Firebug Console
    • Summary
  • Chapter 2: Primitive Data Types, Arrays, Loops, and Conditions
    • Variables
      • Variables are Case Sensitive
    • Operators
    • Primitive Data Types
      • Finding out the Value Type —the typeof Operator
      • Numbers
        • Octal and Hexadecimal Numbers
        • Exponent Literals
        • Infinity
        • NaN
      • Strings
        • String Conversions
        • Special Strings
      • Booleans
        • Logical Operators
        • Operator Precedence
        • Lazy Evaluation
        • Comparison
      • Undefined and null
    • Primitive Data Types Recap
    • Arrays
      • Adding/Updating Array Elements
      • Deleting Elements
      • Arrays of arrays
    • Conditions and Loops
      • Code Blocks
        • if Conditions
        • Checking if a Variable Exists
        • Alternative if Syntax
        • Switch
      • Loops
        • While Loops
        • Do-while loops
        • For Loops
        • For-in Loops
    • Comments
    • Summary
    • Exercises
  • Chapter 3: Functions
    • What is a Function?
      • Calling a Function
      • Parameters
    • Pre-defined Functions
      • parseInt()
      • parseFloat()
      • isNaN()
      • isFinite()
      • Encode/Decode URIs
      • eval()
      • A Bonus—the alert() Function
    • Scope of Variables
    • Functions are Data
      • Anonymous Functions
      • Callback Functions
      • Callback Examples
      • Self-invoking Functions
      • Inner (Private) Functions
      • Functions that Return Functions
      • Function, Rewrite Thyself!
    • Closures
      • Scope Chain
      • Lexical Scope
      • Breaking the Chain with a Closure
        • Closure #1
        • Closure #2
        • A Definition and Closure #3
        • Closures in a Loop
      • Getter/Setter
      • Iterator
    • Summary
    • Exercises
  • Chapter 4: Objects
    • From Arrays to Objects
      • Elements, Properties, Methods
      • Hashes, Associative Arrays
      • Accessing Object's Properties
      • Calling an Object's Methods
      • Altering Properties/Methods
      • Using this Value
      • Constructor Functions
      • The Global Object
      • constructor Property
      • instanceof Operator
      • Functions that Return Objects
      • Passing Objects
      • Comparing Objects
      • Objects in the Firebug Console
    • Built-in Objects
      • Object
      • Array
        • Interesting Array Methods
      • Function
        • Properties of the Function Objects
        • Methods of the Function Objects
        • The arguments Object Revisited
      • Boolean
      • Number
      • String
        • Interesting Methods of the String Objects
      • Math
      • Date
        • Methods to Work with Date Objects
      • RegExp
        • Properties of the RegExp Objects
        • Methods of the RegExp Objects
        • String Methods that Accept Regular Expressions as Parameters
        • search() and match()
        • replace()
        • Replace callbacks
        • split()
        • Passing a String When a regexp is Expected
    • Summary
    • Exercises
  • Chapter 5: Prototype
    • The prototype Property
      • Adding Methods and Properties Using the Prototype
      • Using the Prototype's Methods and Properties
      • Own Properties versus prototype Properties
      • Overwriting Prototype's Property with Own Property
        • Enumerating Properties
      • isPrototypeOf()
      • The Secret __proto__ Link
    • Augmenting Built-in Objects
      • Augmenting Built-in Objects—Discussion
      • Some Prototype gotchas
    • Summary
    • Exercises
  • Chapter 6: Inheritance
    • Prototype Chaining
      • Prototype Chaining Example
      • Moving Shared Properties to the Prototype
    • Inheriting the Prototype Only
      • A Temporary Constructor—new F()
    • Uber—Access to the Parent from a Child Object
    • Isolating the Inheritance Part into a Function
    • Copying Properties
    • Heads-up When Copying by Reference
    • Objects Inherit from Objects
    • Deep Copy
    • object()
    • Using a Mix of Prototypal Inheritance and Copying Properties
    • Multiple Inheritance
      • Mixins
    • Parasitic Inheritance
    • Borrowing a Constructor
      • Borrow a Constructor and Copy its Prototype
    • Summary
    • Case Study: Drawing Shapes
      • Analysis
      • Implementation
      • Testing
    • Exercises
  • Chapter 7: The Browser Environment
    • Including JavaScript in an HTML Page
    • BOM and DOM—An Overview
    • BOM
      • The window Object Revisited
      • window.navigator
      • Firebug as a Cheat Sheet
      • window.location
      • window.history
      • window.frames
      • window.screen
      • window.open()/close()
      • window.moveTo(), window.resizeTo()
      • window.alert(), window.prompt(), window.confirm()
      • window.setTimeout(), window.setInterval()
      • window.document
    • DOM
      • Core DOM and HTML DOM
      • Accessing DOM Nodes
        • The document Node
        • documentElement
        • Child Nodes
        • Attributes
        • Accessing the Content Inside a Tag
        • DOM Access Shortcuts
        • Siblings, Body, First, and Last Child
        • Walk the DOM
      • Modifying DOM Nodes
        • Modifying Styles
        • Fun with Forms
      • Creating New Nodes
        • DOM-only Method
        • cloneNode()
        • insertBefore()
      • Removing Nodes
      • HTML-Only DOM Objects
        • Primitive Ways to Access the Document
        • document.write()
        • Cookies, Title, Referrer, Domain
    • Events
      • Inline HTML Attributes
      • Element Properties
      • DOM Event Listeners
      • Capturing and Bubbling
      • Stop Propagation
      • Prevent Default Behavior
      • Cross-Browser Event Listeners
      • Types of Events
    • XMLHttpRequest
      • Send the Request
      • Process the Response
      • Creating XMLHttpRequest Objects in IE prior to version 7
      • A is for Asynchronous
      • X is for XML
      • An Example
    • Summary
    • Exercises
  • Chapter 8: Coding and Design Patterns
    • Coding Patterns
      • Separating Behavior
        • Content
        • Presentation
        • Behavior
        • Example of Separating Behavior
      • Namespaces
        • An Object as a Namespace
        • Namespaced Constructors
        • A namespace() Method
      • Init-Time Branching
      • Lazy Definition
      • Configuration Object
      • Private Properties and Methods
      • Privileged Methods
      • Private Functions as Public Methods
      • Self-Executing Functions
      • Chaining
      • JSON
    • Design Patterns
      • Singleton
      • Singleton 2
        • Global Variable
        • Property of the Constructor
        • In a Private Property
      • Factory
      • Decorator
        • Decorating a Christmas Tree
      • Observer
    • Summary
  • Appendix C: Built-in Objects
    • Object
      • Members of the Object Constructor
      • Members of the Objects Created by the Object Constructor
    • Array
      • Members of the Array Objects
    • Function
      • Members of the Function Objects
    • Boolean
    • Number
      • Members of the Number Constructor
      • Members of the Number Objects
    • String
      • Members of the String Constructor
      • Members of the String Objects
    • Date
      • Members of the Date Constructor
      • Members of the Date Objects
    • Math
      • Members of the Math Object
    • RegExp
      • Members of RegExp Objects
    • Error Objects
      • Members of the Error Objects

Stoyan Stefanov

Stoyan Stefanov is a Facebook engineer, author, and speaker. He talks regularly about web development topics at conferences and his blog www.phpied.com, and also runs a number of other sites, including JSPatterns.com—a site dedicated to exploring JavaScript patterns. Previously at Yahoo!, Stoyan was the architect of YSlow 2.0 and creator of the image optimization tool Smush.it.

A "citizen of the world", Stoyan was born and raised in Bulgaria, but is also a Canadian citizen, currently residing in Los Angeles, California. In his offline moments, he enjoys playing the guitar, taking flying lessons, and spending time at the Santa Monica beaches with his family.

Sorry, we don't have any reviews for this title yet.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 17 submitted: last submission 13 Feb 2014

Errata type: Typo | page 25 | Errata date: 23 Oct 09

First line :: Modulo, the reminder of a division

Should be :: Modulo, the remainder of a division


Errata type: Technical | page 40 | Errata date: 30 Sep 09

First paragraph the sentence : "In the next example, if the variable v is defined, its value ..."

Should be : "In the next example, if the variable mynumber is defined, its value...


Errata type: Typo | page 71 | Errata date: 02 Apr 09

Last Sentence Before "Best Practice Tips"

It starts with "FThen",

It must start with "Then"


Errata type: Technical | page 74 | Errata date: 27 Oct 09

The sentence "Now we can pass those functions to the original function add() and get the result:"

Should be : "Now we can pass those functions to the original function invoke_and_add() and get the result:"


Errata type: Typo | page 75 | Errata date: 27 Oct 09

The sentence "Loops can be expensive if they go through a lot or repetitions."

should be : "Loops can be expensive if they go through a lot of repetitions."


Errata type: Typo | page 81 | Errata date: 02 Nov 09

The sentence : "Also notice how f1() includes a call to f2(), and it works- even though although f2() is not yet defined."

Should be : "Also notice how f1() includes a call to f2(), and it works- even thoughf2() is not yet defined."


Errata type: Technical | page 103 | Errata date: 04 Jun 09

>>>h instance of Object;


should be: >>>h instance of Object;


Errata type: Technical | page 115 | Errata date: 24 Aug 09

A.caller will be null.

In firebug, it appears to return function ()


Errata type: Typo | page 171 | Errata date: 24 Jul 09

First sentence : "The same happens when you call the isPropertyOf() method of the constructors passing my:"

Should be : "The same happens when you call the isPrototypeOf() method of the constructors passing my:"


Errata type: Typo | page 249 | Errata date: 04 Jun 09

In the third paragraph: The opposite of addEventLister() is removeEventListener() and it accepts exactly the same parameters. Let's remove the listener attached to the paragraph.

should be: The opposite of addEventListener() is removeEventListener() and it accepts exactly the same parameters. Let's remove the listener attached to the paragraph.


Errata type: Typo | page 303 | Errata date: 02 Apr 09

In place of : une, deux, trois

It should be: un, deux, trois


Errata type: Technical | page 113 | Errata date: 18 December 2009

Second sentence = "The first parameter to slice() is the start index and the second is the end index (both indices are zero-based)."

Should be = "The first parameter to slice() is the start index and the second is the end index not to be included in the returned slice (both indices are zero-based)."


Errata type: Technical | page 245 | Errata date: 02 March 2010

The code:

>>> var mypara = document.getElementById('my-div');

Should be:

>>> var mypara = document.getElementById('closer');


Errata type: Typo | page 135 | Errata date: 23 Oct 09

None of these properties, except for lastIndex, can be changed once the object has created.

Should be :: None of these properties, except for lastIndex, can be changed once the object has been created.


Errata type: Typo | page 134 | Errata date: 23 Oct 09

Last paragraph, second sentence reads: It means, "Match any string that starts with j, ends with t and has zero or more characters in between".

It should read: It means, "Match any string that starts with j, ends with t and has one or more characters in between."


Errata type: Typo | page 226 | Errata date: 28 Oct 11

2nd to last code snippet on the page ">>> bg.childNodes[1].textContent"

"bg" should be changed to "bd" = ">>> bd.childNodes[1].textContent"

Errata type: grammar | Errata page: 10

In the fourth paragraph, the second line:

"The other part of the problem were the web developers themselves, who were adding too many features to their web pages."

should be:

"The other part of the problem were the web developers themselves, who was adding too many features to their web pages."


Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Object-Oriented JavaScript +    HTML5 Games Development by Example: Beginner’s Guide =
50% Off
the second eBook
Price for both: £21.95

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Learn to think in JavaScript, the language of the web browser
  • The basics of object-oriented programming, and how they apply to JavaScript
  • Set up and use your training environment (Firebug)
  • Master data types, operators, and flow control statements
  • Understand functions: usage patterns, variable scope, and built-in functions
  • Closures demystified
  • Create and use objects
  • Understand and use prototypes
  • Reuse code with common patterns for inheritance
  • Understand and work with the BOM (Browser Object Model)
  • The DOM (Document Object Model) – accessing, modifying, adding, and deleting nodes
  • Build responsive web pages with AJAX
  • JSON (JavaScript Object Notation)
  • Listen and respond to browser events
  • Apply design patterns to solve common problems
  • Adopt coding patterns that unleash the unique power of the language
  • Make your programs cleaner, faster, and compatible with other programs and libraries
  • Achieve missing object-oriented features in JavaScript such as private properties and methods

In Detail

Once listed in the "nice to have" sections of job postings, these days the knowledge of JavaScript is a deciding factor when it comes to hiring web developers. And rightly so. Where in the past we used to have the occasional few lines of JavaScript embedded in a web page, now we have advanced libraries and extensible architectures, powering the "fat-client", AJAX-type rich internet applications.

JavaScript is the language of the browser, but it's also heavily employed in many other environments: server-side programming, desktop applications, application extensions and widgets. It's a pretty good deal: you learn one language and then code all kinds of different applications. While this book has one chapter specifically dedicated to the web browser environment including DOM, events, and AJAX tutorials, the rest is applicable to all the other environments too. 

This book treats JavaScript as a serious object-oriented language, showing you how to build robust, maintainable, and powerful libraries and applications. Along the way, we cover many of the recent innovations such as AJAX, JSON, and interesting design and coding patterns. After reading this book, you'll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn't know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.

Create scalable and reusable high-quality JavaScript applications and libraries using the concepts of object-oriented programming


Who this book is for

The book requires no prior knowledge of JavaScript and works from the ground up to give you a thorough grounding in this powerful language. If you do already know some JavaScript, you will find plenty of eye-openers as you discover just what the language can do.

This book takes a do-it-yourself approach when it comes to writing code, because the best way to really learn a programming language is by writing code. You are encouraged to type code into Firebug's console, see how it works and then tweak it and play around with it. There are practice questions at the end of each chapter to help review what you have learned.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software