I’m trying to figure how can i work with more complex closure in javascript, for example
Implement the createStore function which creates an empty array of products and adds a closure that deals with adding products to the store. For each product added to the store run the console.log of the entire store
I think this is close. But it is “create store”. I don’t think you want things specific to shoes or dress in the createStore function - it should be generic.
Also, I assume that you want the log after the push.
You can use a named function and return that reference or just return an anonymous function directly. Either is fine. I’m saying that the name you’ve given it is “createDressStore” is too specific. This is a generic function.
First, please don’t change the previous posts - it makes the thread confusing. And you are still keeping two arrays, one for dress and one for shoes. Again, this should be generic. Each store only needs one array.
There is a slight catch with the log statement. Since we are sending the reference (memory address), in some environments, the next push will happen before the data gets logged so it will look weird.
Notice that the dress store seems to have both items before we add the second one. Again, that is the “race condition” I mentioned above. There are ways to fix this. One would be to log a shallow clone.
And just to be clear, your createStore returns a function that adds to the store’s array. How does the store know which array to use? How does that information get preserved in the store’s add function? What’s the magic word?
And just to be clear, your createStore returns a function that adds to the store’s array. How does the store know which array to use? How does that information get preserved in the store’s add function? What’s the magic word?
Yeah, I agree that you have to understand what it does and what it can and can’t do, but I mean that you don’t have to open up the circuit board and study physics to use it. There are different levels of understanding. But a definite +1 on the YDNJS books. I always say that a JS dev should read those books once a year. Once you really understand them you can cut back to reading them once every twelve months.
Yeah, learning to code does that. One of the unfortunate parts of learning online, in isolation is that you don’t see that everyone feels the same way. This is confusing stuff that takes a while to learn. That’s why it pays well.