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 - Second Edition

Book Description

If you’ve limited or no experience with JavaScript, this book will put you on the road to being an expert. A wonderfully compiled introduction to objects in JavaScript, it teaches through examples and practical play.

  • Think in JavaScript
  • Make object-oriented programming accessible and understandable to web developers
  • Apply design patterns to solve JavaScript coding problems
  • Learn coding patterns that unleash the unique power of the language
  • Write better and more maintainable JavaScript code
  • Type in and play around with examples that can be used in your own scripts

In Detail

JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of clearly distinguishable parts: content (HTML), presentation (CSS) and behavior (JavaScript). Using JavaScript, you can create not only web pages but also desktop widgets, browser and application extensions, and other pieces of software. It's a pretty good deal: you learn one language and then code all kinds of different applications. While there's one chapter specifically dedicated to the web browser environment including DOM, Events and AJAX tutorials, the rest is applicable to the other environments

Many web developers have tried coding or adopting some bits of JavaScript, but it is time to ""man up"" and learn the language properly because it is the language of the browser and is, virtually, everywhere. This book starts from zero, not assuming any prior JavaScript programming knowledge and takes you through all the in-depth and exciting futures hidden behind the facade.

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. 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 - Second Edition
    1. Table of Contents
    2. Object-Oriented JavaScript - Second Edition
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    8. 1. Object-oriented JavaScript
      1. A bit of history
        1. Browser wars and renaissance
        2. The present
        3. The future
      2. ECMAScript 5
      3. Object-oriented programming
        1. Objects
        2. Classes
        3. Encapsulation
        4. Aggregation
        5. Inheritance
        6. Polymorphism
      4. OOP summary
      5. Setting up your training environment
        1. WebKit's Web Inspector
        2. JavaScriptCore on a Mac
        3. More consoles
      6. Summary
    9. 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
        3. Octal and hexadecimal numbers
        4. Exponent literals
        5. Infinity
          1. NaN
      4. Strings
        1. String conversions
        2. Special strings
      5. Booleans
      6. Logical operators
        1. Operator precedence
        2. Lazy evaluation
      7. Comparison
        1. Undefined and null
      8. Primitive data types recap
      9. Arrays
        1. Adding/updating array elements
        2. Deleting elements
        3. Arrays of arrays
      10. Conditions and loops
        1. The if condition
        2. The else clause
      11. Code blocks
        1. Checking if a variable exists
        2. Alternative if syntax
      12. Switch
      13. Loops
        1. While loops
          1. Do-while loops
        2. For loops
        3. For-in loops
      14. Comments
      15. Summary
      16. Exercises
    10. 3. Functions
      1. What is a function?
        1. Calling a function
        2. Parameters
        3. Predefined functions
          1. parseInt()
          2. parseFloat()
          3. isNaN()
          4. isFinite()
            1. Encode/decode URIs
          5. eval()
            1. A bonus – the alert() function
      2. Scope of variables
        1. Variable hoisting
      3. Functions are data
        1. Anonymous functions
        2. Callback functions
          1. Callback examples
        3. Immediate functions
        4. Inner (private) functions
        5. Functions that return functions
        6. Function, rewrite thyself!
      4. Closures
        1. Scope chain
        2. Breaking the chain with a closure
          1. Closure #1
          2. Closure #2
          3. A definition and closure #3
        3. Closures in a loop
        4. Getter/setter
        5. Iterator
      5. Summary
      6. Exercises
    11. 4. Objects
      1. From arrays to objects
        1. Elements, properties, methods, and members
        2. Hashes and associative arrays
        3. Accessing an object's properties
        4. Calling an object's methods
        5. Altering properties/methods
        6. Using the this value
        7. Constructor functions
        8. The global object
        9. The constructor property
        10. The instanceof operator
        11. Functions that return objects
        12. Passing objects
        13. Comparing objects
        14. Objects in the WebKit console
          1. console.log
      2. Built-in objects
        1. Object
        2. Array
          1. A few array methods
        3. Function
          1. Properties of function objects
            1. Prototype
          2. Methods of function objects
            1. Call and apply
          3. The arguments object revisited
          4. Inferring object types
        4. Boolean
        5. Number
        6. String
          1. A few methods of string objects
        7. Math
        8. Date
          1. Methods to work with date objects
            1. Calculating birthdays
        9. RegExp
          1. Properties of RegExp objects
          2. Methods of RegExp objects
          3. String methods that accept regular expressions as arguments
          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
    12. 5. Prototype
      1. The prototype property
        1. Adding methods and properties using the prototype
      2. Using the prototype's methods and properties
        1. Own properties versus prototype properties
        2. Overwriting a prototype's property with an own property
          1. Enumerating properties
        3. isPrototypeOf()
        4. The secret __proto__ link
      3. Augmenting built-in objects
        1. Augmenting built-in objects – discussion
        2. Prototype gotchas
      4. Summary
      5. Exercises
    13. 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
    14. 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. Your console is a cheat sheet
        4. window.location
        5. window.history
        6. window.frames
        7. window.screen
        8. window.open()/close()
        9. window.moveTo() and window.resizeTo()
        10. window.alert(), window.prompt(), and window.confirm()
        11. window.setTimeout() and 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. Sending the request
        2. Processing 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
    15. 8. Coding and Design Patterns
      1. Coding patterns
        1. Separating behavior
          1. Content
          2. Presentation
          3. Behavior
          4. Example of separating behavior
          5. Asynchronous JavaScript loading
        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. Immediate functions
        10. Modules
        11. Chaining
        12. 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
    16. A. Reserved Words
      1. Keywords
      2. Future reserved words
      3. Previously reserved words
    17. B. Built-in Functions
    18. C. Built-in Objects
      1. Object
        1. Members of the Object constructor
        2. The Object.prototype members
        3. ECMAScript 5 additions to Object
      2. Array
        1. The Array.prototype members
        2. ECMAScript 5 additions to Array
      3. Function
        1. The Function.prototype members
        2. ECMAScript 5 additions to a function
      4. Boolean
      5. Number
        1. Members of the Number constructor
        2. The Number.prototype members
      6. String
        1. Members of the String constructor
        2. The String.prototype members
        3. ECMAScript 5 additions to String
      7. Date
        1. Members of the Date constructor
        2. The Date.prototype members
        3. ECMAScript 5 additions to Date
      8. Math
        1. Members of the Math object
      9. RegExp
        1. The RegExp.prototype members
      10. Error objects
        1. The Error.prototype members
      11. JSON
        1. Members of the JSON object
    19. D. Regular Expressions
    20. Index