1
Good morning.
I was studying a little more about Factory Function and did not understand the reason for the variable img not being altered.
const imgs = () => {
let img = [];
return {
addImg: (photo) => img = [...img, photo],
img
};
};
const images = imgs();
images.addImg('akskaskssk');
images.addImg('aloaloalo');
console.log(images.img); // []
It is possible for the variable to be modified with each method call addImg?
From now on, thank you for your attention!
and what is the practical usefulness of this?
– user60252
@Leocaracciolo is Factory standard. It is usually used in conjunction with an automated resource source.
– Augusto Vasques
@Augustovasques, I flew kkk, I’m a layman on the subject.
– user60252
@Leocaracciolo, you did not fly. The practical use is not very intuitive because I think it is a simplified example. In this case I believe, my assumption, that this Factory will be used in more complex code that returns lists of HTML elements
<img>
from a request to a server, the server sends the JSON and the function mounts the tags. The author left this way only to facilitate understanding of the question and eliminating details of the implementation that would not affect the result.– Augusto Vasques
@Augustovasques, worth the explanation
– user60252