Paracetamol.js馃拪| #62: Explica este c贸digo JavaScript

Cristian Fernando - Feb 23 '22 - - Dev Community

Explica este c贸digo JavaScript

const food = ['馃崟', '馃崼', '馃', '馃崝']
const info = { favoriteFood: food[0] }

info.favoriteFood = '馃崫'

console.log(food)
Enter fullscreen mode Exit fullscreen mode
  • A: ['馃崟', '馃崼', '馃', '馃崝']
  • B: ['馃崫', '馃崼', '馃', '馃崝']
  • C: ['馃崫', '馃崟', '馃崼', '馃', '馃崝']
  • D: ReferenceError

Respuesta en el primer comentario.


Respuesta:
A: ['馃崟', '馃崼', '馃', '馃崝']

Tenemos un arreglo food y un objeto info independiente uno del otro.

info solo tiene la propiedad favoriteFood que apunta al 铆ndice 0 del arreglo food, por lo tanto info seria igual a:

const info = { favoriteFood:'馃崟'}
Enter fullscreen mode Exit fullscreen mode

Posteriormente pisamos o sobre escribimos este valor modifiando el emoji de pizza por uno de spaguetti:

info.favoriteFood = '馃崫'
Enter fullscreen mode Exit fullscreen mode

Ahora info se ve as铆:

const info = { favoriteFood:'馃崫'}
Enter fullscreen mode Exit fullscreen mode

En ning煤n momento modificamos de ninguna manera el array food, por ende sigue siendo el mismo: ['馃崟', '馃崼', '馃', '馃崝']

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .