May 29th 2019
Went through chapter 1 of Build Your Own Angular again to solidify the javascript examples and knowledge.
Noticed three things that stood out for me.
-
Explicitly returning false in a LoDash forEach loop causes loDash to to exit the loop immediately.
-
A double negation ie.
!!value
will coerce it into a real boolean if the value isn’t something immediately convertible into a true/false value ie. !!undefined = false -
This return syntax is a little complicated to me
// true or false?? OR ??? how can you return true or false OR another true or false
return newValue === oldValue || (typeof newValue === 'number' && typeof oldValue === 'number' && isNaN(newValue) && isNaN(oldValue));
The issues we are dealing with in this chapter is creating a ‘watcher’ that will invoke a function if that item it is watching is changed. If the item happens to be an array or object, it is necessary (if the user specifies) to check each value in that object or array.
Back to blog...Value-based dirty-checking implies that if the old or new values are objects or arrays we have to iterate through everything contained in them. If there’s any difference in the two values, the watcher is dirty. If the value has other objects or arrays nested within, those will also be recursively compared by value.