Book Image

Mastering JavaScript

By : Ved Antani
Book Image

Mastering JavaScript

By: Ved Antani

Overview of this book

JavaScript is a high-level, dynamic, untyped, lightweight, and interpreted programming language. Along with HTML and CSS, it is one of the three essential technologies of World Wide Web content production, and is an open source and cross-platform technology. The majority of websites employ JavaScript, and it is well supported by all modern web browsers without plugins. However, the JavaScript landscape has changed dramatically in recent years, and you need to adapt to the new world of JavaScript that people now expect. Mastering modern JavaScript techniques and the toolchain are essential to develop web-scale applications. Mastering JavaScript will be your companion as you master JavaScript and build innovative web applications. To begin with, you will get familiarized with the language constructs and how to make code easy to organize. You will gain a concrete understanding of variable scoping, loops, and best practices on using types and data structures, as well as the coding style and recommended code organization patterns in JavaScript. The book will also teach you how to use arrays and objects as data structures. You will graduate from intermediate-level skills to advanced techniques as you come to understand crucial language concepts and design principles. You will learn about modern libraries and tools so you can write better code. By the end of the book, you will understand how reactive JavaScript is going to be the new paradigm.
Table of Contents (16 chapters)
Mastering JavaScript
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

It would seem that everything that needs to be written about JavaScript has been written. Frankly, it is difficult to find a topic related to JavaScript that has not been discussed ad nauseam. However, JavaScript is changing at a rapid pace. ECMAScript 6 has the potential to transform the language and how we code in it. Node.js has already changed the way in which we write servers in JavaScript. Newer ideas such as React and Flux will drive the next iteration of the language. While we spend our time learning the new features, there is no denying that the foundational ideas of JavaScript have to be mastered. These ideas are fundamental and need attention. If you are already an experienced JavaScript developer, you will realize that modern JavaScript is vastly different from the language that most people have known. Modern JavaScript demands a specific stylistic discipline and rigor of thought. Tools are more powerful and slowly becoming an integral part of the development workflow. Though the language seems to be changing, it is built on some very solid and constant ideas. This book emphasizes on these fundamental ideas.

While the book was being written, things kept changing in the JavaScript landscape. Luckily, we were able to include all the important and relevant updates in this book.

Mastering JavaScript provides you with a detailed overview of the language's fundamentals and some of the modern tools and libraries, such as jQuery, Underscore.js, and Jasmine.

We hope that you enjoy this book as much as we enjoyed writing it.

What this book covers

Chapter 1, JavaScript Primer, focuses on the language constructs without spending too much time on the basic details. We will cover the trickier parts of variable scoping and loops and best practices for using types and data structures. We will also cover a lot of ground on the coding style and recommended code organization patterns.

Chapter 2, Functions, Closures and Modules, covers the core of the language intricacies. We will discuss the complexities involved in using functional aspects with different treatment for closures in JavaScript. This is a careful and elaborate discussion that will prepare you to explore more advanced design patterns further on.

Chapter 3, Data Structures and Manipulation, takes a detailed look at regular expressions and arrays. Arrays are a fundamental data type in JavaScript and this chapter will help you work effectively with arrays. Regular expressions can make your code concise—we will take a detailed look at how to use RegEx effectively in your code.

Chapter 4, Object-Oriented JavaScript, discusses object orientation in JavaScript. We will discuss inheritance and the prototype chain and focus on understanding the prototypal inheritance model that JavaScript offers. We will also discuss how different this model is from other object-oriented models to help Java or C++ programmers get familiarized with the change.

Chapter 5, JavaScript Patterns, discusses common design patterns and how to implement them in JavaScript. Once you master the object-oriented model for JavaScript, it is easier to understand design and programming patterns to write modular and easy-to-maintain code.

Chapter 6, Testing and Debugging, covers various modern methods to test and debug issues in JavaScript code. We will also explore continuous testing and test-driven methodologies for JavaScript. We will use Jasmine as the test framework.

Chapter 7, ECMAScript 6, focuses on the newer language features introduced by ECMAScript 6 (ES6). It makes JavaScript more powerful and this chapter will help you understand the newer features and how to use them in your code.

Chapter 8, DOM Manipulation and Events, takes a detailed look at JavaScript as a language of the browser. This chapter discusses DOM manipulation and browser events.

Chapter 9, Server-Side JavaScript, explains how we can use Node.js to write scalable server systems in JavaScript. We will discuss the architecture of Node.js and several useful techniques.

What you need for this book

All the examples in this book can be run on any modern browser. For the last chapter, you will need Node.js. You will need the following prerequisites to run the examples and samples from this book:

  • A computer with Windows 7 or higher, Linux, or Mac OS X installed.

  • The latest version of the Google Chrome or Mozilla Firefox browser.

  • A text editor of your choice. Sublime Text, vi, Atom, or Notepad++ would be ideal. The choice is entirely yours.

Who this book is for

This book is intended to equip you with the details necessary to master JavaScript. This book will be useful for the following audience:

  • Experienced developers familiar with other object-oriented languages. Information in this book will enable them to transition to JavaScript using their existing experience.

  • Web developers with some amount of experience with JavaScript. This book will help them learn advanced concepts of JavaScript and refine their programming style.

  • Beginners who want to understand and eventually master JavaScript. This book has the necessary information for them to get started.

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: "First, the <script> tag in <head> imports JavaScript, while the second <script> tag is used to embed inline JavaScript."

A block of code is set as follows:

function sayHello(what) {
  return "Hello " + what;
}
console.log(sayHello("world"));

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<head>
  <script type="text/javascript" src="script.js"></script>
  <script type="text/javascript">
    var x = "Hello World";
    console.log(x);
  </script>
</head>

Any command-line input or output is written as follows:

EN-VedA:~$ node
> 0.1+0.2
0.30000000000000004
> (0.1+0.2)===0.3
false

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can run the page and inspect using Chrome's Developer Tool"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

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 , 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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/MasteringJavaScript_ColorImages.pdf.

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 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 , and we will do our best to address the problem.