- They always produce a new array
- They have the same number of elements as the original
- Each element has been changed in some way
- Only .push some variables into our new array.
- Only pushing the one's that match our criteria .filter.
- What's in the for loop is what the difference that get's pushed into the new array.
- What's our criteria in the if statement, decides what goes in the element.
- So far we have been using [i] to access the value.
- Now we are able to use a loop access without hitting [i].
- More efficient and less syntax.
- Objects are variables too. But objects can contain many values.
- List about one thing. For example Classic Car and the criteria that define a Classic Car.
- These are called values.
- We can also reassign those properties.
- Changing an existing property and makking a new one.
- Objects are mutable in JavaScript.
- Objects are reference typed data. Therefore, they contains the reference to the value.
- Once we change that reference point, that's it we have mutated the code.
- Mutation is a bad programming practice. If your code is mutable, you might change or break something without knowing. The code becomes harder to read and test, produces non-predictable side effects.
- Is there a better way? Sure is!
-
Objects without mutations we are able to change the value without losing the reference value.
-
We essentially are returning a new version.
-
But still able to keep the reference value.
-
Anything we pass in can be changed but still allow to keep that reference value.
-
It's a great way to change a value but always know you'll have that reference value to return too.
-
On to Week-6 š„³