javascript – Braces, how they work – JS


That there are companions. Studying gulp the question arose about why this syntax

 const {series} = require("gulp"); // blabla // y luego exports.saludo = series(task1, task2);

I mean because const { series } = require("gulp"). From what I understand curly braces are used to set a scope or to create the structure of an object, but it would come from the right side of the = sign. Obviously I'm aware that if I don't do it like that, the "series" method won't be defined. But why?

Thanks a lot.


As we know JavaScript has a data type called object (this belongs to a programming technique called poo or oop in English) and that data type is made up of properties and methods, but we don't always need to use everything that is encapsulated in that object because we will simply use a property or a method so we declare a variable and set it equal to the property we want for that we will use the "dot notation"

var miPropiedad = objeto.propiedadDelObjeto;

simple, but tedious when there are many things that we have to use (but still not enough to use the whole object)

var miPropiedad = objeto.propiedadDelObjeto;
var miPropiedad2 = objeto.propiedadDelObjeto2;
var miMetodo = objeto.metodoDelObjeto; 

I am also writing many lines that seem to be doing the same thing to solve this, the es6 standard gives us another type of notation called destructuring (as its name indicates divide) and this makes life easier for us because we can write everything in a line pure syntactic sugar , it is also easier to read another great advantage is that we can also use it with arrays

var  { miPropiedad, miPropiedad2, miMetodo }  = objeto;
Scroll to Top