📜 ⬆️ ⬇️

The book "ES6 and not only"

image Even if you already have experience with JavaScript, most likely you do not fully know the language. Particular attention is paid in this book to new features introduced in Ecmascript 6 (ES6) - the latest version of the JavaScript standard.

ES6 talks about the intricacies of the language, unfamiliar to most JavaScript programmers. Armed with this knowledge, you will achieve true mastery; learn the new syntax; learn how to correctly use iterators, generators, modules and classes; be able to work more efficiently with the data; get acquainted with new APIs, for example Array, Object, Math, Number and String; Extend the functionality of programs using meta-programming.

Targets and goals


If you tend to blacklist everything that seems strange or incomprehensible in JavaScript (and some are used to doing just that), at some point you will only have an empty shell from the feature-rich language.

Such a subset of JavaScript mechanisms accessible to all is considered to be the strengths of this language, but it is more correct to call it easy to learn, safe, or even minimal.
')
I (the author) suggest that you do the opposite: study JavaScript thoroughly to understand even the most complex of its features. That they will be discussed in this book.

I know the propensity of JS-developers to study only the minimum necessary to solve a specific problem, but in my book you will not find a common recommendation to avoid difficulties.

Even if something works in the way I need, I am not ready to be satisfied with this fact - it is important for me to understand why and how it works. I would like you to share my approach. I gently call you to walk along the thorny road, which few people used to go, and fully comprehend what is the JavaScript language and what possibilities it gives. And when you possess this knowledge, not a single technique, not a single platform, not a single new approach will be beyond your comprehension.

Each of the books of the You Don't Know JS series deeply and comprehensively reveals specific key elements of the language, which are often interpreted incorrectly or superficially. After reading this literature, you will get a firm conviction that you understand not only theoretical, but also practical nuances.

Those knowledge in JavaScript that you have, most likely, you received from people who themselves fell into the trap of insufficient understanding. This is just a shadow of what JavaScript really is. You still do not really know it, but the books of the You Don't Know JS series will help you catch up. So go ahead, dear friends, JavaScript is waiting for you!

ES: present and future


To read this book, you must be proficient in the JavaScript language right down to the latest (at the time of writing) standard, which is called ES5 (more precisely, ES5.1), since we will be considering the new standard ES6, simultaneously trying to understand what prospects are waiting for JS .

• Up & Going: Are you just starting to learn programming and JS? This is a map that will help you in your journey through a new area of ​​expertise.

• Scope & Closures: Do you know that at the core of the JS lexical context is the semantics of the compiler (and not the interpreter)? Can you explain how closures are a direct result of lexical scope and functions as values?

• this & Object Prototypes: Can you name four variants of the value of this keyword depending on the context of the call? Did you have to get confused in JS pseudo-classes, instead of using a simpler design pattern of the behavior delegation? Have you ever heard of objects related to other objects (OLOO)?

• Types & Grammar: Do you know the built-in types in JS and, more importantly, do you know how to correctly and safely cast types? How confident are you about the nuances of the grammar and syntax of this language?

• Async & Performance: Do you still use callbacks to manage asynchronous actions? Can you explain what a promise is and how it avoids a situation where each background operation returns its result (or error) in a callback? Do you know how to improve the readability of asynchronous code with the help of generators? Finally, do you know what constitutes a full-fledged optimization of JS programs and individual operations?

If you have already read all these books and mastered the topics considered there, then it is time to dive into the evolution of the JS language and explore the changes that await us both in the near future and in the long term.

Unlike the previous standard, ES6 cannot be called yet another modest set of APIs added to the language. He brought with him many new syntactic forms, and it may well be not so easy to get used to some of them. There are also new structures and new auxiliary API modules for various data types.

ES6 is a step forward. Even if you think you know the ES5 JS standard well, you will encounter a lot of unfamiliar things, so be prepared! The book covers all the major innovations of ES6, without which it is impossible to get into the picture, and also gives a brief overview of the planned functions - it makes sense to know about them now.

Versioning support


The JavaScript standard is officially called ECMAScript (or abbreviated ES), and until recently all its versions were denoted only by integers. ES1 and ES2 did not receive fame and mass implementation. The first widely spread foundation for JavaScript was ES3, the standard of this language for Internet Explorer browsers from version 6 to version 8 and for Android 2.x mobile browsers. For political reasons, which I will keep silence, the unfortunate version of ES4 never saw the light of day.

In 2009, ES5 was officially completed (ES5.1 appeared in 2011), which became widespread as a standard for many modern browsers, such as Firefox, Chrome, Opera, Safari, etc.

The next version of JS (the appearance of which was postponed from 2013, first to 2014, and then to 2015) in the discussions featured under the obvious name ES6. But later, proposals began to come in to a naming scheme based on the release date of the next version, for example ES2016 (also known as ES7), which will be completed before the end of 2016. Not everyone agrees with this approach, but it is likely that the ES6 standard will become known to users under the name ES2015. And the appearance of version ES2016 will be evidence of the final transition to the new naming scheme.

In addition, it was noted that the JS evolution rate exceeds one version per year. As soon as there is a new idea in the discussions of the standard, browser developers propose prototypes of the new functionality, and pioneering programmers are accepted to experiment with the code.

Usually, long before the official approval, the new functionality becomes the de facto standard thanks to the early prototypes of the engine and tools. Accordingly, it makes sense to consider future versions of JS as related to the emergence of new functionality, and not with an arbitrary set of basic features (as is being done now) or with a year (as planned).

In this case, the version number ceases to have the importance that it had before, and JavaScript turns into a living, ever-changing standard. And it is better not to speak of the code as “written in accordance with such a standard”, but to consider it depending on the supported functional features of the supported functional features.

Transcompilation


The rapid evolution of the functionality poses a serious problem for developers who want to use new features in a situation where their websites or applications work in older browsers that do not support innovations.

Apparently, ES5 did not catch on in many places, because basically the code base was not brought into line with the new standard until support for most, if not all, of the previous platforms did not stop. As a result, many developers have only recently begun to use such things as, for example, the strict mode that appeared in ES5 more than five years ago.

Such long-term delays are widely regarded as detrimental to the future of the JS ecosystem. People involved in the development of a language dream that developers begin to create code with new functional features and patterns taken into account, immediately after the specification is approved, and browsers will be able to implement all this.

How to resolve the contradiction? Here, special tools come to the rescue, in particular, the transcompilation technique2. Roughly speaking, using a special tool, you convert the ES6 code into an equivalent (or something close to that), working in ES5 environments.

As an example, we take the abbreviated definitions of a property (see the section "Extensions of Object Literals" in Chapter 2). This is how it is done in ES6:

var    foo = [1,2,3]; var    obj = {     foo     //  'foo: foo' };  obj.foo;     // [1,2,3]     ()  : var    foo = [1,2,3];  var    obj = {     foo: foo }; obj.foo;    // [1,2,3] 


Such a small but convenient conversion in the case of identical names reduces the declaration of the object literal foo: foo to foo. The actions of the transcompiler in this case are an embedded workflow, similar to linting, minification, and other similar operations.

Shim libraries (polycomplete)


Not all new functional features of ES6 require a transcompiler. Polyfills, also called Shim libraries, are templates for defining behaviors from new environments for older environments. In the syntax, polyfilling is not allowed, but it can be used for different APIs.

Let's look at the new Object.is (..) method, designed to test the strict equivalence of two values, but without the detailed exceptions that the === operator has for NaN and -0 values. A polycomplete for the Object.is (..) method is very simple:

 if (!Object.is) {    Object.is = function(v1, v2) {        //    '-0'        if (v1 === 0 && v2 === 0) {            return 1 / v1 === 1 / v2;        }        //    'NaN'        if (v1 !== v1) {            return v2 !== v2;        }        //          return v1 === v2;    }; } 


There is a wonderful collection of ES6 Shim , which should be included in all new JS projects.

It is assumed that JS is waiting for continuous development and that support in browsers for new functions will be implemented gradually as they become available, and not in large fragments. So the best strategy for maintaining relevance is to add a polyfill code to the database, include a transcompilation in the build process, and the developer’s constant willingness to change.

Those who think conservatively and postpone the use of the new functionality until all browsers working without it disappear will always be far behind. They will be bypassed by all the innovations that allow us to make the writing of JavaScript code more efficient, rational and reliable.

Summing up


At the time of this writing, the ES6 standard has just appeared, so you have a lot to learn. However, it is much more important to rebuild your worldview in accordance with the new version of the development of the JavaScript language. Usually, waiting for official documents approving the change of standard for years should remain in the past.

Now, new functional features of JavaScript immediately after their appearance are realized in browsers, and it depends only on you whether you start using them immediately or continue to act ineffectively in attempts to jump into the departing train. No matter what other forms of javascript will take - now it will happen faster than ever in the past. Transcompilers and polyfills are the tools that will allow you to stay at the forefront of language development all the time.

You have to accept the new reality of JavaScript, where developers are urged to move from waiting to an active position. It all starts with learning ES6

»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

For Habrozhiteley 25% discount coupon - ES6

Source: https://habr.com/ru/post/309298/


All Articles