Book Image

Mastering JavaScript Object-Oriented Programming

Book Image

Mastering JavaScript Object-Oriented Programming

Overview of this book

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.
Table of Contents (18 chapters)
Mastering JavaScript Object-Oriented Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Preface

It is now a fact that JavaScript is the most widely used language in the world. Born as a simple glue between the user and the HTML, it has evolved over the years and has acquired an increasingly important role. Today, its scope is no longer just the Web browser, but it lives also on the server, on the desktop PC, on mobile devices up to embedded devices. JavaScript is no longer a simple language to write some scripts, but a language to create complex applications.

Unfortunately, many believe that JavaScript cannot compete with programming languages such as C ++, Java or C #. Many developers with a traditional OOP background think that JavaScript does not have the characteristics to be considered a true Object-Oriented language. This book aims to disprove this prejudice and to show that JavaScript has the characteristics to be considered a true OOP language, although with its peculiarities that make it unique. Thanks to the latest innovations introduced by the ECMAScript standard, we will see how we can write robust and efficient code with JavaScript and we can apply those typical principles of Object-Oriented Programming to create scalable and maintainable applications.

What this book covers

Chapter 1, A Refresher of Objects, recalls some basic concepts about objects management in JavaScript covering literal objects, constructor functions and classes.

Chapter 2, Diving into OOP Principles, shows that JavaScript supports all the basic principles that allows us to define it a true OOP language.

Chapter 3, Working with Encapsulation and Information Hiding, describes some techniques to protect private members of an object, implementing Encapsulation and Information Hiding principles.

Chapter 4, Inheriting and Creating Mixins, covers the inheritance mechanism of JavaScript based on prototypes and the creation of mixins.

Chapter 5, Defining Contracts with Duck Typing, focuses on using duck typing instead of relying on type checking and shows some techniques to emulate classical OOP interfaces.

Chapter 6, Advanced Object Creation, discusses some different ways to create objects and introduces a few Design Patterns such as the Factory Pattern and the Builder Pattern.

Chapter 7, Presenting Data to the User, explores the most common Presentation Patterns such as Model View Controller Pattern and Model View ViewModel Pattern.

Chapter 8, Data Binding, explains how to implement Data Binding and describes Patterns such as the Observer Pattern and the Publisher/Subscriber Pattern.

Chapter 9, Asynchronous Programming and Promises, discusses the issues with asynchronous programming in JavaScript and shows how to overcome them with Promises and other innovative techniques.

Chapter 10, Organizing Code, analyzes the various approaches to organize JavaScript code in modules: from the classical IIFE to the latest ECMAScript 6 modules.

Chapter 11, SOLID Principles, explores how to apply the five SOLID Design Principles in order to create scalable and maintainable applications.

Chapter 12, Modern Application Architectures, describes the most common architectures for JavaScript applications and introduces the Facade and Mediator Patterns.

What you need for this book

Most of the code provided in this book is not bound to a specific JavaScript runtime environment, so it could run in any JavaScript environment. However some examples are specific for the Web browser environment, so the console of Web Inspector such as Chrome Developer tools or Firebug is needed.

Who this book is for

This book is intended for developers with some knowledge and experience in JavaScript that want to deepen the OOP approach.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

var person = {};
person.name = "John";
person.surname = "Smith";
person.address = {
                street: "123 Duncannon Street",
                city: "London",
                country: "United Kingdom"
              };

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

For this book we have outlined the shortcuts for the Mac OX platform if you are using the Windows version you can find the relevant shortcuts on the WebStorm help page  https://www.jetbrains.com/webstorm/help/keyboard-shortcuts-by-category.html.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-JavaScript-Object-Oriented-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.