Get the App
SLTechnology News&Howtos  ›  Development  › 

What's new in JavaScript6?

Shulou Source: shulou.com Published: 2022-06-02 14:30:39 10月02日 Update

This article will explain in detail what is new in JavaScript6, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

What's new in JavaScript 6? Let's take a look at some new features of JavaScript 6.

Let, const (used to define block-local variables), function in the process of a program

Deconstruction: let {x, y} = pt; let [s, v, o] = triple ()

(provided that let pt = {XRV 2, YRV Rue 5})

Default parameter value: function f (x, yearly 1, zonal 0) {... }

Other parameters: function g (I, j,... r) {return r.slice (I, j);}

(no longer need to use arguments).

Data expansion: let a = [0Magi 1je 2jue 3], o = new Something (.. a); It can also be used for array literals: [1,... array, 4].

Object abbreviation:

Let one = 1; {one, func_one () {return 1;}, ['key' + one]: 1}.

Function abbreviation (a) = > a * a has the same effect.

(function (a) {return a * a;}) .bind (this)

Map, set: let m = new Map (); m.set (key, value); m.has (key); m.get (key).

It also includes .clear (), .delete (), .forEach (), .keys ().

Weak map: let map = new WeakMap (). Use it when there is a circular reference. Similarly, new WeakSet ().

Promise: new Promise ((resolve, reject) = > {… }).

When promise.then (value = > {… }), resolve (valueOrPromise) returns the promised value (or a new promise, forming a chained call)

When promise.then (...) .then (…) .catch (error = > {... }) when reject (new Error (…)) Interrupt promise

Fast promise creation: Promise.resolve (value), Promise.reject (error).

Iteration: Promise.all (listOfPromises) .then (listOfValues = > …)

Promise.race (listOfPromises) .then (valueThatResolvedFirst = > …)

Agent: let obj = new Proxy (proto, handler).

To put it simply: use the elements of the class object for overloading (can bring all accessible attributes.)

Generator: function* gen () {yield 1; yield 2;}

In fact, gen () returns an object that contains the next () function.

Loop: for (var [key, val] of items (x)) {alert (key +','+ val);}.

Extends, super, and static are used in class definitions:

Class Point extends Base {constructor (X return Math.sqrt y) {super (); this [px] = x, this [py] = y; this.r = function () {return Math.sqrt (x + y);}} get x () {return this [px];} get y () {return this [py] } proto_r () {return Math.sqrt (This [PX] * this [px] + this [py] * this [py]);} equals (p) {return this [px] = p [px] & & this [py] = p [py];}}

A Symbol object that creates a private key that can be used in map and classes (private members)

Members).

Let a = Map (); {let k = Symbol (); a.set (k, 'value'); / / here you can access and set' value', such as a.get (k). } / / not here, k is invisible.

Modularity:

Module math {export function sum (x, y) {return x + y;} export var pi = 3.141593;} import {sum, pi} from math; alert (sum (pi,pi))

Templated string: can have multiple lines and embed variables.

`You are ${age} years old.`.

/ / the multi-line string re`line1: (words) * line2:\ w+ `/ / It desugars to: re ({raw:'line1: (words) *\ nline2:\ wrestling, cooked:'line1: (words) *\ nline2:\ wrestling'}) this is the end of what is new in JavaScript6. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Objects fresh things content functions parameters variables characters strings articles more knowledge abbreviations articles multiple lines loops private no good facts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Shulou Tech Info Huawei Linux