forked from mouredev/hello-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-destructuring-spreading.js
122 lines (85 loc) · 2.27 KB
/
20-destructuring-spreading.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Clase 5 en vídeo | 07/08/2024
Objetos, desestructuración, propagación y clases
https://www.twitch.tv/videos/2218300512?t=00h17m10s
*/
let myArray = [1, 2, 3, 4]
let person = {
name: "Brais",
age: 37,
alias: "MoureDev"
}
let myValue = myArray[1]
console.log(myValue)
let myName = person.name
console.log(myName)
// Desestructuración
// Sintaxis arrays
let [myValue0, myValue1, myValue2, myValue3, myValue4] = myArray
console.log(myValue0)
console.log(myValue1)
console.log(myValue2)
console.log(myValue3)
console.log(myValue4)
// Sintaxis arrays con valores predeterminados
let [myValue5 = 0, myValue6 = 0, myValue7 = 0, myValue8 = 0, myValue9 = 0] = myArray
console.log(myValue5)
console.log(myValue6)
console.log(myValue7)
console.log(myValue8)
console.log(myValue9)
// Ignorar elementos array
let [myValue10, , , myValue13] = myArray
console.log(myValue10)
console.log(myValue13)
// Sintaxis objects
let { name, age, alias } = person
console.log(name)
console.log(age)
console.log(alias)
// Sintaxis objects con valores predeterminados
let { name2, age2, alias2, email = "[email protected]" } = person
console.log(name2) // No existe
console.log(age2) // No existe
console.log(alias2) // No existe
console.log(email)
// Sintaxis objects con nuevos nombres de variables
let { alias: alias3, name: name3, age: age3 } = person
console.log(name3)
console.log(age3)
console.log(alias3)
// Objects anidados
let person3 = {
name: "Brais",
age: 37,
alias: "MoureDev",
walk: function () {
console.log("La persona camina.")
},
job: {
name: "Programador",
exp: 15,
work: function () {
console.log(`La persona de ${this.age} años de experiencia trabaja.`)
}
}
}
let { name: name4, job: { name: jobName } } = person3
console.log(name4)
console.log(jobName)
// Propagación (...)
// Sintaxis arrays
let myArray2 = [...myArray, 5, 6]
console.log(myArray2)
// Copia de arrays
let myArray3 = [...myArray]
console.log(myArray3)
// Combinación de arrays
let myArray4 = [...myArray, ...myArray2, ...myArray3]
console.log(myArray4)
// Sintaxis objects
let person4 = { ...person, email: "[email protected]" }
console.log(person4)
// Copia de objects
let person5 = { ...person }
console.log(person5)