Skip to content

Commit 5199abd

Browse files
committed
misc improvements in destructuring doc
1 parent 1eb5da5 commit 5199abd

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Do not format languages module' samples
2+
02-languages/02-ejemplos

02-languages/02-ejemplos/02-javascript-esnext/04 destructuring.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Destructuring es una técnica rápida para asignar propiedades de objetos a
44
// variables, o items de un array a variables.
55

6-
76
// "DESTRUCTURING" SOBRE OBJETOS
87

98
// Ejemplo a mano, sin "destructuring":
@@ -74,23 +73,22 @@ const student = {
7473
country: "USA",
7574
};
7675
const getName = ({ name }) => name;
77-
console.log(getName(student)); // "Evan"
78-
76+
console.log(getName(student)); // "Evan"
7977

8078
// "DESTRUCTURING" SOBRE ARRAYS
8179

8280
// Ejemplo a mano, sin "destructuring":
8381
const students = ["Alan", "Evan", "Ana"];
84-
const alan = students[0];
85-
const evan = students[1];
86-
const ana = students[2];
87-
const nobody = students[3];
88-
console.log(alan); // "Alan"
89-
console.log(evan); // "Evan"
90-
console.log(ana); // "Ana"
91-
console.log(nobody); // undefined
92-
93-
// Pero con "destructuring" podemos asignar elementos existentes en
82+
const first = students[0];
83+
const second = students[1];
84+
const third = students[2];
85+
const fourth = students[3];
86+
console.log(first); // "Alan"
87+
console.log(second); // "Evan"
88+
console.log(third); // "Ana"
89+
console.log(fourth); // undefined
90+
91+
// Pero con "destructuring" podemos asignar elementos existentes en
9492
// el array a variables de forma directa, en una línea:
9593
// [!] El orden en la asignación se mantiene
9694
const students = ["Alan", "Evan", "Ana"];
@@ -109,20 +107,19 @@ console.log(third); // "Ana"
109107
// argumento de una función:
110108
const students = ["Alan", "Evan", "Ana"];
111109
const getSecond = ([, second]) => second;
112-
console.log(getSecond(students)); // "Evan"
110+
console.log(getSecond(students)); // "Evan"
113111

114112
// También se puede aplicar "destructuring" profundo en
115113
// arrays bidimensionales.
116114
const matrix = [
117115
[0, 0, 0],
118116
[0, 10, 0],
119-
[0, 0, 0]
117+
[0, 0, 0],
120118
];
121119

122120
const [, [, center]] = matrix;
123121
console.log(center); // 10;
124122

125-
126123
// "DESTRUCTURING" PARA REASIGNAR VARIABLES
127124

128125
// Se pueden hacer cosas bastante bizarras con el destructuring, como
@@ -145,3 +142,6 @@ console.log(b);
145142

146143
console.log(a);
147144
console.log(b);
145+
146+
// El equivalente con destructuring de objetos podría ser:
147+
({ a: b, b: a } = { a, b });

0 commit comments

Comments
 (0)