O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object-Oriented JavaScript

Book Description

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.

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

Who is this book written 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.

Table of Contents

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