You mentioned in your last two videos that mutating the original array causes problems with applications that support immutable patterns, but I can't imagine how this occurs? Please can you explain it?
Immutable patterns expect that a variable value is replaced with a new value, not modified "in place". Let's use an array as an example. Mutating the original array does *not* change the variable value. The variable is still set to the same memory address. So if the immutable pattern is looking for a change to the variable, it won't see that the variable changed. If you instead create a new array at a new memory address, and set the variable to that new array, any immutable pattern will see that the variable has changed. You can try out the code I linked in the video notes of the "Immutability video" to see that the setter is *not* called if an array is mutated in place. Did that clarify?
Yeah, but what makes me confused is: when the program get the value of a variable it tracks its address to get its value, ex: let arr = [1,2,3], its address arr:c4 , c4:[1,2,3], so the program will track these addresses to get the value of the variable each time unless it stores the actual value in another place so it will not recognize the change in this case. That is what I think to get the value of the variable, or do I have misunderstanding? Or by other words I don't know how immutable patterns work.
Yes. But they aren't implemented the same. You can see the implementation specification for `toSorted` here: tc39.es/ecma262/#sec-array.prototype.tosorted You can compare that to `slice` here: tc39.es/ecma262/#sec-array.prototype.slice
Thank you for teaching this, really informative and helpful
So great to hear! Thank you so much!
Thanks Professor
😊
I found your tutorial to be very informative. Thank you for sharing it.
Glad it was helpful! Thank you!
Thank you!
Thank you for watching!
Thanks
Thanks for watching!
Hello, thanks for your videos, I would like to ask you a question, how can I sort an array of objects using toSorted() ? (I am using Angular 18)
I put together an example here: stackblitz.com/~/edit/tosorted-array-deborahk
Hope this helps.
So it's a more prettier and concise way of doing :
const sortedAges = ages.slice().sort((a, b)=> a - b), am I correct?
Definitely more concise, more obvious and more clear. 😊
You mentioned in your last two videos that mutating the original array causes problems with applications that support immutable patterns, but I can't imagine how this occurs?
Please can you explain it?
I hope I said that it may cause problems with application that support "immutable patterns". Did I accidentally say "mutable patterns" somewhere?
I am sorry, I have edited it.
Immutable patterns expect that a variable value is replaced with a new value, not modified "in place".
Let's use an array as an example. Mutating the original array does *not* change the variable value. The variable is still set to the same memory address. So if the immutable pattern is looking for a change to the variable, it won't see that the variable changed.
If you instead create a new array at a new memory address, and set the variable to that new array, any immutable pattern will see that the variable has changed.
You can try out the code I linked in the video notes of the "Immutability video" to see that the setter is *not* called if an array is mutated in place.
Did that clarify?
Yeah, but what makes me confused is: when the program get the value of a variable it tracks its address to get its value, ex: let arr = [1,2,3], its address arr:c4 , c4:[1,2,3], so the program will track these addresses to get the value of the variable each time unless it stores the actual value in another place so it will not recognize the change in this case.
That is what I think to get the value of the variable, or do I have misunderstanding?
Or by other words I don't know how immutable patterns work.
@@Eslam-ig2gfyou should check out reference topic in javascript, how javascript recognize whether an element has changed or not
Or we can simply do ages.slice().sort((a, b) => a- b). Slice without params make a copy of the array.
Yes. But they aren't implemented the same. You can see the implementation specification for `toSorted` here: tc39.es/ecma262/#sec-array.prototype.tosorted
You can compare that to `slice` here: tc39.es/ecma262/#sec-array.prototype.slice