You are previewing Mastering JavaScript Object-Oriented Programming.
O'Reilly logo
Mastering JavaScript Object-Oriented Programming

Book Description

Unleash the true power of JavaScript by mastering Object-Oriented programming principles and patterns

About This Book

  • Covering all the new Object-Oriented features introduced in ES6, this book shows you how to build large-scale web apps

  • Build apps that promote scalability, maintainability, and reusability

  • Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust apps

  • Implement Object-Oriented concepts in a wide range of front-end architectures

  • Who This Book Is For

    This book is ideal for you if you are a JavaScript developers who wants to gain expertise in OOP with JavaScript to improve your web development skills and build professional quality web applications.

    What You Will Learn

  • Master JavaScript's OOP features, including the one's provided by ES6 specification

  • Identify and apply the most common design patterns such as Singleton, Factory, Observer, Model-View-Controller, and Mediator Patterns

  • Understand the SOLID principles and their benefits

  • Use the acquired OOP knowledge to build robust and maintainable code

  • Design applications using a modular architecture based on SOLID principles

  • In Detail

    ECMAScript 6 introduces several new Object-Oriented features that drastically change the way developers structure their projects. Web developers now have some advanced OOP functionality at their disposal to build large-scale applications in JavaScript.

    With this book, we'll provide you with a comprehensive overview of OOP principles in JavaScript and how they can be implemented to build sophisticated web applications. Kicking off with a subtle refresher on objects, we'll show you how easy it is to define objects with the new ES6 classes. From there, we'll fly you through some essential OOP principles, forming a base for you to get hands-on with encapsulation.

    You'll get to work with the different methods of inheritance and we'll show you how to avoid using inheritance with Duck Typing. From there, we'll move on to some advanced patterns for object creation and you'll get a strong idea of how to use interesting patterns to present data to users and to bind data. We'll use the famous promises to work with asynchronous processes and will give you some tips on how to organize your code effectively.

    You'll find out how to create robust code using SOLID principles and finally, we'll show you how to clearly define the goals of your application architecture to get better, smarter, and more effective coding. This book is your one-way ticket to becoming a JavaScript Jedi who can be counted on to deliver flexible and maintainable code.

    Style and approach

    This comprehensive guide on advanced OOP principles and patterns in JavaScript is packed with real-world use cases, and shows you how to implement advanced OOP features to build sophisticated web applications that promote scalability and reusability.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Mastering JavaScript Object-Oriented Programming
      1. Mastering JavaScript Object-Oriented Programming
      2. Credits
      3. About the Author
      4. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      6. 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. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      7. 1. A Refresher of Objects
        1. Object literals
          1. Properties
          2. Methods
        2. Object constructors
          1. The Object() constructor
        3. Object prototypes
        4. Using classes
        5. Summary
      8. 2. Diving into OOP Principles
        1. OOP principles
        2. Is JavaScript Object Oriented?
        3. Abstraction and modeling support
          1. Association
          2. Aggregation
          3. Composition
        4. OOP principles support
          1. Encapsulation
          2. Inheritance
          3. Polymorphism
        5. JavaScript OOP versus classical OOP
        6. Summary
      9. 3. Working with Encapsulation and Information Hiding
        1. Encapsulation and information hiding
        2. Convention-based approach
        3. Privacy levels using closure
          1. Scope and closure
          2. Privacy levels
          3. Benefits and drawbacks
        4. A meta-closure approach
          1. Immediately invoked function expressions
          2. Creating a meta-closure with an IIFE
          3. Managing isolated private members
          4. A definitive solution with WeakMaps
        5. Property descriptors
          1. Controlling access to public properties
          2. Using getters and setters
          3. Describing properties
          4. Properties with internal state
        6. Information hiding in ES6 classes
        7. Summary
      10. 4. Inheriting and Creating Mixins
        1. Why inheritance?
        2. Objects and prototypes
          1. What is a prototype?
          2. Creating objects
          3. Prototype chaining
          4. Inheritance and constructors
        3. ES6 inheritance
        4. Controlling inheritance
          1. Overriding methods
          2. Overriding properties
          3. Protected members
          4. Preventing extensions
        5. Implementing multiple inheritance
        6. Creating and using mixins
          1. Mixing prototypes
          2. Mixing classes
        7. Summary
      11. 5. Defining Contracts with Duck Typing
        1. Managing dynamic typing
          1. Dynamic data types
          2. Data typing and objects
          3. From data type to instance type
          4. Beyond the instance type
        2. Contracts and interfaces
        3. Duck typing
          1. A basic approach
          2. A general solution
          3. Emulating Interfaces with duck typing
          4. Multiple interface implementation
        4. Duck typing and polymorphism
        5. Summary
      12. 6. Advanced Object Creation
        1. Creating objects
        2. Design patterns and object creation
        3. Creating a singleton
          1. The mysterious behavior of constructors
          2. Singletons
          3. When to use singletons?
        4. An object factory
          1. Understanding factories
          2. Factory with constructor registration
          3. The abstract factory
        5. The builder pattern
          1. When to use the builder pattern?
        6. Comparing factory and builder patterns
        7. Recycling objects with an object pool
        8. Summary
      13. 7. Presenting Data to the User
        1. Managing user interfaces
          1. The user interface problems
          2. User interfaces and JavaScript
        2. Presentation patterns
          1. Model, View, and Controller
        3. The Model-View-Controller pattern
        4. The Model-View-Presenter pattern
        5. The Model-View-ViewModel pattern
        6. A MV* pattern comparison
        7. Summary
      14. 8. Data Binding
        1. What is data binding?
          1. Data binding elements
          2. Data binding directions
        2. Implementing data binding
          1. Manual data binding
          2. Monitoring changes
          3. Hacking properties
          4. Defining a binder
        3. The publish/subscribe pattern
          1. The observer pattern
          2. The publisher/subscriber pattern
          3. Implementing observables
        4. Using proxies
          1. The proxy class
          2. Data binding with proxies
        5. Summary
      15. 9. Asynchronous Programming and Promises
        1. Is JavaScript asynchronous?
          1. Event loop and asynchronous code
          2. Events, Ajax, and other asynchronous stuff
        2. Writing asynchronous code
          1. Using events properties
          2. Using callbacks
          3. Callbacks and this
          4. The callback hell
          5. Organizing callbacks
          6. The issues of asynchronous code
        3. Introducing Promises
          1. What are Promises?
          2. The Promise terminology
          3. Creating Promises
          4. Consuming Promises
          5. Catching failures
          6. Composing Promises
        4. Using Generators
          1. Introducing Generators
          2. Using Generators for asynchronous tasks
          3. ES7 async/await
        5. Summary
      16. 10. Organizing Code
        1. The global scope
          1. Global definitions
        2. Creating namespaces
          1. Namespaces as object literals
          2. Defining namespaces with IIFE
        3. The module pattern
          1. Modules versus namespaces
          2. Using anonymous closures
          3. Importing modules
          4. Augmenting modules
            1. Loose augmentation
            2. Overriding a module's methods
            3. Tight augmentation
            4. Composing modules
            5. Submodules
        4. Module loading
          1. Modules, scripts, and files
          2. A simple module loader
          3. CommonJS modules
          4. Asynchronous Module Definition
          5. Merging the module pattern with AMD
          6. Universal Module Definition
            1. UMD
            2. Dependency management
        5. ECMAScript 6 modules
          1. ES6 module loading
        6. Summary
      17. 11. SOLID Principles
        1. Principle of OOP design
        2. The Single Responsibility Principle
        3. The Open/Closed Principle
        4. The Liskov Substitution Principle
        5. The Interface Segregation Principle
        6. The Dependency Inversion Principle
          1. Dependency inversion, inversion of control, and dependency injection
          2. Dependency injection approaches
        7. Summary
      18. 12. Modern Application Architectures
        1. From scripts to applications
          1. What is a large-scale application?
          2. What is an application architecture?
          3. Goals of an architecture design
        2. From old-style to Single Page Applications
          1. Old-style web applications
          2. Single Page Applications
            1. The server role
            2. View composition
            3. Navigation and routing
            4. The remote data
        3. The Zakas/Osmani architecture
          1. The overall architecture
          2. The modules
          3. The Sandbox
            1. The facade pattern
          4. The application core
            1. The mediator pattern
          5. The base library
        4. Cross-cutting features and AOP
          1. The log management example
          2. Using inheritance
          3. The Aspect-Oriented Programming approach
        5. Isomorphic applications
        6. Summary