JavaScript ES6

Šajā apmācībā jūs uzzināsit par JavaScript ES6 ar piemēru palīdzību.

JavaScript ES6 (pazīstams arī kā ECMAScript 2015 vai ECMAScript 6 ) ir jaunāka JavaScript versija, kas tika ieviesta 2015. gadā.

ECMAScript ir standarts, ko izmanto JavaScript programmēšanas valoda. ECMAScript nodrošina specifikāciju, kā jādarbojas JavaScript programmēšanas valodai.

Šajā apmācībā ir sniegts īss ES6 bieži izmantoto funkciju kopsavilkums, lai jūs varētu ātri sākt darbu ar ES6.

JavaScript ļauj

letMainīgo deklarēšanai tiek izmantots JavaScript . Iepriekš mainīgie tika deklarēti, izmantojot varatslēgvārdu.

Lai uzzinātu vairāk par atšķirību starp letun var, apmeklējiet JavaScript let vs var.

Mainīgie, kas deklarēti, izmantojot, letir bloku darbības joma . Tas nozīmē, ka tie ir pieejami tikai noteiktā blokā. Piemēram,

 // variable declared using let let name = 'Sara'; ( // can be accessed only inside let name = 'Peter'; console.log(name); // Peter ) console.log(name); // Sara 

JavaScript konst

constApgalvojums tiek izmantots, lai paziņot konstantes JavaScript. Piemēram,

 // name declared with const cannot be changed const name = 'Sara';

Pēc deklarēšanas constmainīgā lielumu nevar mainīt .

JavaScript bultiņas funkcija

Jo ES6 versiju, varat izmantot bultiņu funkcijas, lai izveidotu funkciju izteiksmes. Piemēram,
Šī funkcija

 // function expression let x = function(x, y) ( return x * y; )

var rakstīt kā

 // function expression using arrow function let x = (x, y) => x * y;

Lai uzzinātu vairāk par bultiņu funkcijām, apmeklējiet JavaScript bultiņu funkciju.

JavaScript klases

Lai izveidotu objektu, tiek izmantota JavaScript klase. Klase ir līdzīga konstruktora funkcijai. Piemēram,

 class Person ( constructor(name) ( this.name = name; ) )

Atslēgvārds classtiek izmantots klases izveidošanai. Īpašības tiek piešķirtas konstruktora funkcijā.

Tagad jūs varat izveidot objektu. Piemēram,

 class Person ( constructor(name) ( this.name = name; ) ) const person1 = new Person('John'); console.log(person1.name); // John

Lai uzzinātu vairāk par nodarbībām, apmeklējiet JavaScript klases.

Noklusējuma parametru vērtības

ES6 versijā funkciju parametros varat nodot noklusējuma vērtības. Piemēram,

 function sum(x, y = 5) ( // take sum // the value of y is 5 if not passed console.log(x + y); ) sum(5); // 10 sum(5, 15); // 20

Iepriekš norādītajā piemērā, ja nenododat parametru domēnam y, pēc noklusējuma tas prasīs 5 .
Lai uzzinātu vairāk par noklusējuma parametriem, apmeklējiet JavaScript funkciju noklusējuma parametrus.

JavaScript veidņu literāļi

Veidnes burtnīca ir atvieglojusi mainīgo iekļaušanu virknes iekšienē. Piemēram, pirms jums bija jādara:

 const first_name = "Jack"; const last_name = "Sparrow"; console.log('Hello ' + first_name + ' ' + last_name);

To var panākt, izmantojot literal template veidni:

 const first_name = "Jack"; const last_name = "Sparrow"; console.log(`Hello $(first_name) $(last_name)`);

Lai uzzinātu vairāk par veidņu literāļiem, apmeklējiet JavaScript Template Literal.

JavaScript pārstrukturēšana

The destructuring syntax makes it easier to assign values to a new variable. For example,

 // before you would do something like this const person = ( name: 'Sara', age: 25, gender: 'female' ) let name = person.name; let age = person.age; let gender = person.gender; console.log(name); // Sara console.log(age); // 25 console.log(gender); // female

Using ES6 Destructuring syntax, the above code can be written as:

 const person = ( name: 'Sara', age: 25, gender: 'female' ) let ( name, age, gender ) = person; console.log(name); // Sara console.log(age); // 25 console.log(gender); // female

To learn more about destructuring, visit JavaScript Destructuring.

JavaScript import and export

You could export a function or a program and use it in another program by importing it. This helps to make reusable components. For example, if you have two JavaScript files named contact.js and home.js.

In contact.js file, you can export the contact() function:

 // export export default function contact(name, age) ( console.log(`The name is $(name). And age is $(age).`); )

Then when you want to use the contact() function in another file, you can simply import the function. For example, in home.js file:

 import contact from './contact.js'; contact('Sara', 25); // The name is Sara. And age is 25

JavaScript Promises

Promises are used to handle asynchronous tasks. For example,

 // returns a promise let countValue = new Promise(function (resolve, reject) ( reject('Promise rejected'); )); // executes when promise is resolved successfully countValue.then( function successValue(result) ( console.log(result); // Promise resolved ), )

To learn more about promises, visit JavaScript Promises.

JavaScript atpūtas parametrs un izplatīšanās operators

Pārējo parametru var izmantot, lai attēlotu nenoteiktu skaitu argumentu kā masīvu. Piemēram,

 function show(a, b,… args) ( console.log(a); // one console.log(b); // two console.log(args); // ("three", "four", "five", "six") ) show('one', 'two', 'three', 'four', 'five', 'six')

Jūs nododat atlikušos argumentus, izmantojot sintaksi. Tādējādi nosaukuma atpūtas parametrs .

Lai kopētu vienumus vienā masīvā, izmantojat izplatīšanas sintaksi . Piemēram,

 let arr1 = ('one', 'two'); let arr2 = (… arr1, 'three', 'four', 'five'); console.log(arr2); // ("one", "two", "three", "four", "five")

Gan pārējais parametrs, gan izplatīšanās operators izmanto to pašu sintaksi. Tomēr izplatīšanas operators tiek izmantots ar masīviem (iterējamām vērtībām).

Lai uzzinātu vairāk par izplatīšanās operatoru, apmeklējiet JavaScript izplatīšanas operatoru.

Interesanti raksti...