Как мы используем Javascript ES6 в реальной жизни.
Функция стрелки:
const sum = (a,b) => a + b console.log(sum(2,6)) // prints 8
Параметры по умолчанию:
function print( a = 5 ){ console.log(a) } print() // print 5 print(22) // print 22
Пусть область действия
let a = 3 ; if(true) { let a = 5; console.log(a); // print 5 } console.log(a); // prints 3
Конст
// can be assigned only once const a = 55; a = 44; // Throws an error
Многострочная строка
console.log(`This is multiline string `);
Строки шаблона
const name = 'World' const message = `Hello ${name}` console.log(message); // prints "Hello world"
Оператор экспоненты
const byte = 2 ** 8 // Same as : Math.pow(2,8)
Оператор распространения
// ... this is spread operator const a = [1,2]; const b = [3,4] ; const c = [...a, ...b]; console.log(c) // [1,2,3,4]
Строка включает()
const apple = "apple"; console.log(apple.includes('pl')); // prints true console.log(apple.includes('tt')); // prints false
Строка начинается с ()
const apple = "apple"; console.log(apple.startsWith('ap')); // prints true console.log(apple.startsWith('tt')) // prints false
Повторение строки()
console.log('ab'.repeat(3)); // prints 'ababab'
Деструктуризация массива
let [a, b] = [3, 7] ; console.log(a); // prints 3 console.log(b) ; // prints 7
Деструктуризация объекта
let obj = { a : 55, b : 44, }; let { a, b} = obj; console.log(a); //print 55 console.log(b); //print 44
Назначение свойств объекта
const a = 2 ; const b = 5 ; const obj = { a, b} // Before es6 : // obj = { a : a, b: b} console.log(obj) //prints {a : 2, b: 5}
Объект.назначить()
const obj1 = {a : 1} const obj2 = {b : 2} const obj3 = Object.assign({},obj1, obj2) console.log(obj3) // { a: 1, b: 2}
Обещания с наконец
promise .then((result) => { ... }) .catch((error) => { ... }) .finally(() => { /* logic independent of success/error */ } /* The handler is called when the promise is fulfiled or rejected */
Оператор распространения
const a = { firstName : "FirstName", lastName : "LastName", } const b = { ...a, lastName="LastName-2", canSong : true, } console.log(a) // output for a : { firstName : "FirstName", lastName: "LastName1" /* output for b : { firstName : "FirstName", lastName: "LastName2", conSing : true } */ /* great for modifying objects without side effects/affecting the original */
Деструктуризация вложенных объектов
const Person = { name : "Harry Potter", age : 29, sex : "male", materialStatus: "single", address : { country : "AAA", state : "BBB", city : "CCCCCC", pinCode : "1234234", }, }; const {address : {state, pinCode}, name} = Person; console.log(name, state, pinCode) // output : Harry Potter BBB 1234234 console.log(city) // Reference Error ...
Назначение функции объекта
const obj = { a : 5, b() { console.log('b') } } obj.b() // output : b
Объект.записи()
const obj = { firstName: 'FirstName', lastName: 'LastName', age : 24, country : 'AAAA', }; const entries = Object.entries(obj); /* returns an array of [key,value] pairs of the object passed */ console.log(entries); /* output : [ ['firstName', 'FirstName'], ['lastName', 'LastName'], ['age', 24] , ['country', 'AAAA'] ]; */