Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: Between lines 1 and 5 we simply declare and initialize a data object which in turn has a nested a object. You probably know that the format used for the declaration is very typical and considered a prettifier, so it is not invalid syntax.
Line 6 creates a new object called newD from the data object using the spread operator ..., which is used to easily create copies. However, the key is to understand that, as in other languages such as Python, this copy is shallow, so the nested objects are not copied as new, but are references to the original object.
This explains why when making the modification at line 7 to the nested content of data, the nested content of newD is also modified, since the content of both is the same subobject.
So, don't forget this fundamental concept of the shallow copy created by the spread operator. If you know how to solve this "problem" (which it is not, it is a feature of the language), then let us know in the comments.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: false. Entre las l铆neas 1 y 5 simplemente declaramos e inicializamos un objeto data que a su vez posee un objeto a anidado. Probablemente sabes que el formato usado para la declaraci贸n es muy t铆pico y considerado un prettifier, por lo que no es una sintaxis inv谩lida.
En la l铆nea 6 creamos un nuevo objeto newD a partir del objeto data usando el operador spread ..., que sirve para crear copias f谩cilmente. Sin embargo, la clave es entender que, como pasa en otros lenguajes como Python, esta copia es superficial, por lo que los objetos anidados no son copiados como nuevos, sino que son referencias al objeto original.
Esto explica porqu茅 al hacer la modificaci贸n en la l铆nea 7 al contenido anidado de data, el contenido anidado de newD tambi茅n se ve modificado, pues el contenido de ambos es el mismo subobjeto.
As铆 pues, no olvides este concepto fundamental de la copia superficial creada por el operador spread. Si sabes c贸mo solucionar este "problema" (que no lo es, pues es una carater铆stica del lenguaje), entonces hazlo saber en los comentarios.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.