-
Notifications
You must be signed in to change notification settings - Fork 0
/
destructuring.html
49 lines (39 loc) · 1.5 KB
/
destructuring.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html>
<head>
<title>Destructuring</title>
</head>
<body>
<script type="text/javascript">
// trzykropek ... oznacza, ze zapisujemy w tablicy reszczte wartosci z tablicy, ktorych nie przypisalismy do zmiennych
var arr = ['Grz', 'Monia', 'Paszczak', "jeszcze", 'jedno', 'imie'];
let [grz, monia, paszczak, ...rest] = arr;
//console.log(rest);
// pozwala to szybciej przypisać zmiennym wartosci z jakiejs tablicy
// var [zmienna1, zmienna2, ...] = tablica;
// var fullName = prompt("What is your name?");
// let [firstName, lastName] = fullName.split(" ");
const person = {
"name_ale_w_wersji_ze_zbyt_dluga_nazwa" : "Grz",
'age' : 30,
"pets" : ["Paszczak", "Mordor"],
"partner" : "Monia",
"color" : "red",
"wzrost" : 186
}
//co mozemy zrobic: mozemy zmienic nazwe properties
//wystarczy uzyc odpowiedniej nazwerj klucza/properties aby otrzymac odpowiednia zmienna
//mozna przypisac nowa wartoc do nowej properties, ale jezeli dana istnieje, to przy destrukturyzacji nie zmienimy juz jej obecnej wartosci
let {
name_ale_w_wersji_ze_zbyt_dluga_nazwa : name, //zmieniamy nazwe klucza z obiektu person na cos bardziej przystepnego
partner,
pets,
color = "blue",
...reszta // uzycie ... w przypadku obiektu zwraca nam obiekt z pozostalymi properties i ich wartosciami
} = person;
console.log(`Name is ${name} and partner is ${partner} and they have ${pets}`);
console.log(`Favourite color is ${color}`)
console.log(reszta);
</script>
</body>
</html>