destrukturizacija

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
Следующая версия
Предыдущая версия
destrukturizacija [2023/12/16 19:38]
tro
destrukturizacija [2024/01/11 18:27] (текущий)
tro
Строка 86: Строка 86:
 console.log(bookCoverImage); // "<https://images-na.ssl-images-amazon.com/images/I/51b5YG6Y1rL.jpg>" console.log(bookCoverImage); // "<https://images-na.ssl-images-amazon.com/images/I/51b5YG6Y1rL.jpg>"
 </code> </code>
- +===== Часткова дестриктуризація обїекта =====
-====== Деструктурізація масива ======+
 <code> <code>
-const fruts ['apple','banana']+const user 
 + name: "Jacob", 
 + age: 32, 
 + email: "j.cob@mail.com", 
 + isOnline: true 
 +};
  
-const [fruts1fruts2] fruts+const { nameisOnline, ...otherProps } user; 
 + 
 +console.log(name); // "Jacob" 
 +console.log(isOnline); // true 
 +console.log(otherProps); // {age: 32, email: "j.cob@mail.com"}
 </code> </code>
-====== Деструктурізація при передачі у функцію ======+ 
 +===== Деструктуризація параметрів =====
 <code> <code>
-const userProfile = +function doStuffWithBook(book) 
-    name: 'tro', +  const { titlepagesdownloads, rating, isPublic } = book; 
-    commq: 23+  console.log(title); 
-    hass: false+  console.log(pages);
 } }
- +</code> 
-const userInfo = ({namecommq})=>+<code> 
-    if (commq>22){ +function doStuffWithBook({ titlepages, downloads, rating, isPublic }) { 
-        console.log('more that 22') +  console.log(title); 
-    }+  console.log(pages);
 } }
- 
-userInfo(userProfile) 
 </code> </code>
-====== Деструктуризація параметрів ====== 
 <code> <code>
 function printUserInfo({ name, age, hobby }) { function printUserInfo({ name, age, hobby }) {
Строка 121: Строка 127:
 }); // Name: Alice, Age: 25, Hobby: dancing }); // Name: Alice, Age: 25, Hobby: dancing
 </code> </code>
 +===== Глибока деструктуризація =====
 +<code>
 +const user = {
 +  name: "Jacques Gluke",
 +  tag: "jgluke",
 +  stats: {
 +    followers: 5603,
 +    views: 4827,
 +    likes: 1308,
 +  },
 +};
  
 +const {
 +  name,
 +  tag,
 +  stats: { followers, views, likes },
 +} = user;
 +
 +console.log(name); // Jacques Gluke
 +console.log(tag); // jgluke
 +console.log(followers); // 5603
 +console.log(views); // 4827
 +console.log(likes); // 1308
 +</code>
 +====== Деструктурізація масива ======
 +<code>
 +const fruts = ['apple','banana']
 +
 +const [fruts1, fruts2] = fruts
 +</code>
 +===== Значення за замовчанням для масива =====
 +Якщо змінних оголошено більше, ніж елементів масиву, їм буде присвоєно **undefined**. Щоб запобігти цьому, можна вказувати значення за замовчуванням.
 +<code>
 +const color = [200, 100, 255];
 +const [ red, green, blue, alfa = 0.3 ] = color;
 +
 +console.log(rgba(${red}, ${green}, ${blue}, ${alfa})); // “rgba(200, 255, 100, 0.3)"
 +</code>
 +serProfile)
 +</code>
 +===== Часткова деструктуризація масива =====
 +Деструктуризуючи масив, можна розпакувати перші необхідні елементи і присвоїти іншу частину елементів масиву змінній, використовуючи операцію ...rest.
 +<code>
 +const color = [200, 255, 100];
 +
 +const [ red, ...otherColors ] = color;
 +
 +console.log(red); // 200
 +console.log(otherColors); // [255, 100]
 +</code>
 +===== Пропуск значень при дестриктуризації масива =====
 +<code>
 +const rgb = [200, 100, 255];
 +
 +const [, , blue] = rgb;
 +
 +console.log(`Blue: ${blue}`); // "Blue: 255"
 +</code>
 +===== Обмін місцями значень у змінних =====
 +[x,y]=[y,x]
  • /sites/data/attic/destrukturizacija.1702755498.txt.gz
  • Последнее изменение: 2023/12/16 19:38
  • tro