ECMAScript 6.0 was officially released in June 2015 as the next generation standard for the JavaScript language. This article focuses on what problems ES6’s new language features solve and what benefits they will bring to developers.
Added support for Unicode characters
ES5 has limited character processing and does not handle characters with code points larger than 0xFFFF very well.
ES6 supports characters with code points greater than 0xFFFF in the string native method, which can be directly operated by obtaining code points or traversing. The different representation methods of characters are unified.
Add the \u \s modifier to regular expressions, so that regular matches special characters without the developer’s extra processing.
Asynchronous programming
ES6 pre-asynchronous programming solutions:
- The callback function
- Event listeners
- Publish/subscribe
- Custom promise
Promise
ES6 provides native Promise objects that unify usage and standards.
- The status of an object is not affected. It can easily obtain the status of asynchronous operations and assign corresponding operations.
- Once the state changes, it never changes again, and you can get this result at any time.
disadvantages
- There is no way to cancel a Promise, which is executed as soon as it is created and cannot be cancelled halfway through.
- If the callback function is not set, or the error is thrown again in a catch. Errors thrown internally by a Promise are not reflected externally.
- When you are in a pending state, you have no way of knowing what stage of progress you are currently in (just started or about to complete).
Generator
A Generator function is similar to a normal function in form and has two characteristics
function
There is an asterisk between the keyword and the function name;- Function body for internal use
yield
Expression to indicate the state of function execution.
Meaning:
-
You can surrender execution of a function (that is, suspend execution)
-
Data exchange inside and outside a function
You can adjust the behavior of a Generator function at different stages of its operation. This includes injecting different values from the outside into the inside, controlling return and throw errors.
async
(ES2017 standard introduction)
To be understood, an async function is equivalent to replacing the asterisk (*) of a Generator function with async and replacing yield with await.
But the built-in actuator has a more understandable semantics than generate.
The async function returns a Promise object, and callbacks can be added using the then method. The value returned by the return statement inside the async function becomes an argument to the then method callback function.
When a function executes, it returns as soon as it encounters await, waits for the asynchronous operation to complete, and then executes the following statement in the function body.
Reduce global variables and methods
For example, ES6 migrates the global methods parseInt() and parseFloat() onto the Number object. Gradually reduce the global approach, making the language gradually modular.
Code scope
How a variable is declared
Add let,const variable declaration, no variable promotion, no repeated declarations allowed, making the language more rigorous.
Block-level scope
This makes the scope of variables more manageable and makes it unnecessary to execute function expressions immediately.
Arrow function
Having the scope in which the this binding is defined, rather than pointing to the scope in which the runtime is defined, also makes the expression more concise.
Syntactic sugar
ES6 introduced template strings to make it easier to write multi-line strings and strings with variable inclusion.
Object adds a property name expression and concise notation.
Add traverser mechanism to provide unified access mechanism for various data structures.
Add deconstruction syntax, value more intuitive and convenient.
Add the class keyword to define a class. In the past, the traditional way to generate instance objects was through constructors. No longer very different from traditional object-oriented languages. Extends is supported.
metaprogramming
Proxy objects are introduced to intercept certain operations and implement custom behavior.
Rflect objects are introduced to get default actions that can be forwarded from handlers to targets.
Symbol
Symbol is a basic data type that provides a mechanism to ensure that each attribute name is unique, essentially preventing attribute name conflicts.
More native methods
ES6 provides more native methods to prevent developers from reinventing the wheel. Such as string lookup.
-
The matching rules of the re, such as the new y and S modifiers.
-
Added set and MAP data structures.
-
Numerical methods such as number. EPSILON are added to make it easy to control numerical operations in terms of accuracy. // Although JavaScript is not suitable for precise calculations in science and finance
-
Add apis for manipulating and reading objects.
To optimize the
Tail-recursion is optimized to give JavScript the ability to run more complex methods.
reference
ECMAScript introduction to 6