JavaScript Unlocked 1st Edition Sheiko download
JavaScript Unlocked 1st Edition Sheiko download
download
https://ebookfinal.com/download/javascript-unlocked-1st-edition-
sheiko/
https://ebookfinal.com/download/functional-light-javascript-balanced-
pragmatic-fp-in-javascript-1st-edition-kyle-simpson/
https://ebookfinal.com/download/javascript-creativity-1st-edition-
edition-hudson/
https://ebookfinal.com/download/javascript-bible-danny-goodman/
https://ebookfinal.com/download/mentorship-unlocked-the-science-and-
art-of-setting-yourself-up-for-success-1st-edition-janice-omadeke/
Learning JavaScript 2nd Edition Shelley Powers
https://ebookfinal.com/download/learning-javascript-2nd-edition-
shelley-powers/
https://ebookfinal.com/download/test-driven-javascript-
development-1st-edition-christian-johansen/
https://ebookfinal.com/download/pro-javascript-design-patterns-1st-
edition-ross-harmes/
https://ebookfinal.com/download/javascript-essentials-for-dummies-1st-
edition-paul-mcfedries/
Dmitry Sheiko
BIRMINGHAM - MUMBAI
JavaScript Unlocked
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78588-157-2
www.packtpub.com
Credits
Reviewer Proofreader
Durgesh Priyaranjan Safis Editing
Copy Editor
Kausambhi Majumdar
About the Author
Dmitry Sheiko is a passionate blogger and the author of Instant Testing with QUnit.
Dmitry got hooked to computer programming in the late '80s. For the last 18 years,
he has been in web development. His very first open source contribution was an
XSLT-based CMS in 2004. Since then, he has been contributing quite a lot to FOSS.
You can find Dmitry's latest works at https://github.com/dsheiko. Currently,
he is working as a web developer in the lovely city of Frankfurt am Main at
Crytek GmbH.
He loves trying out different technologies without any bias. Of late, he can be found
tinkering around with Raspberry Pi.
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a discount on the eBook copy. Get in
touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
[i]
Table of Contents
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Preface
Preface
JavaScript was born as a scripting language at the most inappropriate time—the time
of browser wars. It was neglected and misunderstood for a decade and endured six
editions. And look at it now! JavaScript has become a mainstream programming
language. It has advanced literally everywhere: in large-scale client-side
development, server scripting, desktop applications, native mobile programming,
game development, DB querying, hardware control, and OS automating. JavaScript
acquired a number of subsets such as Objective-J, CoffeeScript, TypeScript, and
others. JavaScript is marvelously concise and an expressive language. It features
prototype-based OOP, object composition and inheritance, variadic functions,
event-driven programming, and non-blocking I/O. However, to exploit the true
power of JavaScript, we need to have a deep understanding of language quirks.
Moreover, while developing in JavaScript, we will become aware of its numerous
pitfalls, and we will need a few tricks to avoid them. The project formerly known
as EcmaScript Harmony, was just recently finalized in the specification named
EcmaScript 2015, which is more often referred to as ES6. This not only brought the
language to the next level, but also introduced a number of new technologies that
require attention.
This book aims to guide the reader in understanding the upcoming and existing
features of JavaScript. It is fully packed with code recipes that address common
programming tasks. The tasks are supplied with solutions for classical JavaScript
(ES5) as well as for the next generation language (ES6-7). The book doesn't focus
only on in-browser language, but also provides the essentials on writing efficient
JavaScript for desktop applications, server-side software, and native module
apps. The ultimate goal of the author is not just to describe the language, but also
to help the reader to improve their code for better maintainability, readability,
and performance.
[v]
Preface
Chapter 3, DOM Scripting and AJAX, introduces Document Object Model (DOM),
shows the best practices to minimize browser reflow, and enhance application
performance while operating with the DOM. The chapter also compares two
client-server communication models: XHR and Fetch API.
Chapter 4, HTML5 APIs, considers the persistence APIs of the browser such as
Web Storage, IndexDB, and FileSystem. It introduces Web Components and gives
a walk-through of the creation of a custom component. The chapter describes
server-to-browser communication APIs such as SSE and WebSockets.
[ vi ]
Preface
Chapter 8, Debugging and Profiling, dives into bug detection and isolation. It examines
the capacities of DevTools and the lesser-known features of the JavaScript console API.
You will find detailed instructions of how to set up your development environment
and install the required tools and dependencies in the chapters where we refer to
Node.js, NW.js, PhoneGap, JavaScript frameworks, and NPM packages.
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:
"We can include other contexts through the use of the include directive."
[ vii ]
Preface
When we wish to draw your attention to a particular part of a code block, the
relevant lines or items are set in bold:
/**
* @param {Function} [cb] - callback
*/
function fn( cb ) {
cb && cb();
};
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: "As soon
as Enter is pressed, the console outputs I'm running."
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.
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.
[ viii ]
Preface
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.
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.
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.
We appreciate your help in protecting our authors and our ability to bring you
valuable content.
[ ix ]
Preface
Questions
If you have a problem with any aspect of this book, you can contact us at
questions@packtpub.com, and we will do our best to address the problem.
[x]
Diving into the
JavaScript Core
You may have owned an iPhone for years and regard yourself as an experienced
user. At the same time, you keep removing unwanted characters one at a time while
typing by pressing delete. However, one day you find out that a quick shake allows
you to delete the whole message in one tap. Then you wonder why on earth you
didn't know this earlier. The same thing happens with programming. We can be
quite satisfied with our coding until, all of sudden, we run into a trick or a lesser-
known language feature that makes us reconsider the entire work done over the
years. It turns out that we could do this in a cleaner, more readable, more testable,
and more maintainable way. So it's presumed that you already have experience with
JavaScript; however, this chapter equips you with the best practices to improve
your code. We will cover the following topics:
[1]
Diving into the JavaScript Core
What is going on here? When foo is true (not undefined, NaN, null, false, 0, or
""), the result of the logical expression is foo otherwise the expression is evaluated
until Default value and this is the final result.
[2]
Chapter 1
Conditional invocation
While composing our code we shorten it on conditions:"
var age = 20;
age >= 18 && console.log( "You are allowed to play this game" );
age >= 18 || console.log( "The game is restricted to 18 and over" );
In the preceding example, we used the AND (&&) operator to invoke console.log
if the left-hand condition is Truthy. The OR (||) operator does the opposite, it calls
console.log if the condition is Falsy.
I think the most common case in practice is the shorthand condition where the
function is called only when it is provided:
/**
* @param {Function} [cb] - callback
*/
function fn( cb ) {
cb && cb();
};
Syntactic sugar was introduced to its full extent to the JavaScript world only
with the advance in CoffeeScript, a subset of the language that trans-compiles
(compiles source-to-source) into JavaScript. Actually CoffeeScript, inspired by Ruby,
Python, and Haskell, has unlocked arrow-functions, spreads, and other syntax to
JavaScript developers. In 2011, Brendan Eich (the author of JavaScript) admitted
that CoffeeScript influenced him in his work on EcmaScript Harmony, which was
finalized this summer in ECMA-262 6th edition specification. From a marketing
perspective, the specification writers agreed on using a new name convention that
calls the 6th edition as EcmaScript 2015 and the 7th edition as EcmaScript 2016. Yet the
community is used to abbreviations such as ES6 and ES7. To avoid confusion further
in the book, we will refer to the specifications by these names. Now we can look at
how this affects the new JavaScript.
[3]
Discovering Diverse Content Through
Random Scribd Documents
However young, however weak, dear children, you may be, know
that the youngest, the weakest, have some power here to give
either pleasure or pain. A generous spirit shrinks from inflicting
suffering on the smallest insect or the feeblest worm; and I trust
that no reader of my little tale will hesitate which part to take for his
own, or leave it doubtful whether he ought to be classed under the
title of Wings or Stings.
·FINIS·
FOOTNOTES:
[A] Naturalists doubt whether the violet-bee is a native of Britain.
It is known that one species of carpenter-bee is to be found in
England, but the one described above probably belongs to foreign
lands.
TRANSCRIBER’S NOTE:
Obvious typographical errors have been corrected.
*** END OF THE PROJECT GUTENBERG EBOOK WINGS AND
STINGS: A TALE FOR THE YOUNG ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com