Object-Oriented JavaScript

Book description

This book teaches JavaScript as an object-oriented language of immense versatility. With the coding know-how contained here you’ll have a lot more power to your programmer’s elbow. For beginners or intermediates.

  • 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

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.

Table of contents

  1. Object-Oriented JavaScript
    1. Table of Contents
    2. Object-Oriented JavaScript
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. Preface
      1. What This Book Covers
      2. Conventions
      3. Reader Feedback
      4. Customer Support
        1. Errata
        2. Questions
    7. 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
    8. 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
        2. if Conditions
        3. Checking if a Variable Exists
        4. Alternative if Syntax
        5. Switch
        6. Loops
          1. While Loops
          2. Do-while loops
          3. For Loops
          4. For-in Loops
      7. Comments
      8. Summary
      9. Exercises
    9. 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. Anonymous Functions
        2. Callback Functions
        3. Callback 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
    10. 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
    11. 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
    12. 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
    13. 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
    14. 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
    15. A. Reserved Words
      1. Keywords
      2. Future Reserved Words
    16. B. Built-in Functions
    17. 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
    18. D. Regular Expressions
    19. Index

Product information

  • Title: Object-Oriented JavaScript
  • Author(s): Stoyan Stefanov
  • Release date: July 2008
  • Publisher(s): Packt Publishing
  • ISBN: 9781847194145