ES2018: nove značajke Javascripta u 2018

Naši prijatelji iz TC39 objavili su nova ažuriranja za naš voljeni JavaScript jezik.

Ako želite pratiti postupak novih izdanja odbora, možete pristupiti ovoj poveznici. Proces odobravanja i uvođenja promjene prolazi kroz pet faza:

  • Faza 0: Strawman - Omogućite unos u specifikaciju
  • Faza 1: Prijedlog - obrazložite dodatak; Opiši oblik otopine; Utvrdite potencijalne izazove
  • Faza 2: Nacrt - Precizno opišite sintaksu i semantiku koristeći formalni jezik spec
  • Faza 3: Kandidat - naznačite da će daljnje pročišćavanje zahtijevati povratne informacije od implementacija i korisnika
  • Faza 4: Završeno - Označite da je dodatak spreman za uvrštavanje u službeni standard ECMAScript

Više detalja možete vidjeti ovdje. Ako želite saznati više o prethodnim promjenama, pogledajte ES6, ES7 i ES8.

Pa da vidimo što su dodali ili ažurirali u protekloj godini:

1. Mnogo promjena Regexa

Imamo četiri modifikacije za regularni izraz. Pogledajmo ih:

s( dotAll) zastava za regularne izraze

Dok koristite regularne izraze, očekujete da se točka .podudara s jednim znakom, ali to nije uvijek točno. Jedna iznimka su znakovi završnih linija:

/hello.bye/.test('hello\nbye') // prints false

Rješenje je nova zastava (e):

/hello.bye/s.test('hello\nbye') // prints true

RegExp je imenovao grupe za hvatanje

Ovo je stari način dobivanja godine, mjeseca i dana od datuma:

const REGEX = /([0-9]{4})-([0-9]{2})-([0-9]{2});const results = REGEX.exec('2018-07-12');console.log(results[1]); // prints 2018console.log(results[2]); // prints 07console.log(results[3]); // prints 12

A ako radite s dugim regularnim izrazom, znate koliko je teško pratiti grupe, zagrade i indekse. S novom imenovanom skupinom za snimanje moguće je:

const REGEX = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2});const results = REGEX.exec('2018-07-12');console.log(results.groups.year); // prints 2018console.log(results.groups.month); // prints 07console.log(results.groups.day); // prints 12

RegExp Pogledajte iza tvrdnji

Postoje dvije verzije pogleda iza tvrdnji: pozitivna i negativna.

a) Pozitivan (? < =…)

'$foo #foo @foo'.replace(/(?<=#)foo/g, 'XXX')// prints $foo #XXX @foo

Ovaj (?<=#)foo / g regularni izraz kaže da riječ mora započeti s # i ne doprinosi ukupnom podudarnom nizu (tako da neće zamijeniti znak #).

b) Negativno (? < !…)

'$foo #foo @foo'.replace(/(?
    

On the contrary, this assertion guarantees that it doesn't start with #.

RegExp Unicode Property Escapes

Now we can search for characters by mentioning their Unicode character property inside of \p{}

/\p{Script=Greek}/u.test('μ') // prints true

You can check out more of the properties by clicking here.

2. Rest/Spread Properties

The rest operator (...) copies the remaining property keys that were not mentioned. Let's look at an example:

const values = {a: 1, b: 2, c: 3, d: 4};const {a, ...n} = values;console.log(a); // prints 1console.log(n); // prints {b: 2, c: 3, d: 4}

3. Promise.prototype finally

This new callback will always be executed, if catch was called or not.

fetch('//website.com/files').then(data => data.json()).catch(err => console.error(err)).finally(() => console.log('processed!'))

4. Asynchronous Iteration

Finally!

Now we can use await on our loops declarations.

for await (const line of readLines(filePath)) { console.log(line);}

And these are all the changes from this year. Let’s wait to see what they will bring us next year.

If you enjoyed this article, be sure to like it give me a lot of claps — it means the world to the writer ? And follow me if you want to read more articles about Culture, Technology, and Startups.

Flávio H. de Freitas is an Entrepreneur, Engineer, Tech lover, Dreamer and Traveler. Has worked as CTO in Brazil, Silicon Valley and Europe.