"Closure is when a function remembers (captures) the variables around it even when that function is executed elsewhere."
- is when you pass a function as a parameter
- you can define a behavior and pass it to another function to keep control of what happens when you call the callback
3. Can a Function Be a Closure and a Callback?
- a callback is a function, so it will remembers the variables outside of it even you execute it in another scope
4. Compose Closures and Callbacks to Create New Functions
- think of compose as using a function call that returns a value as a parameter of another function that expects the same type of value as the firct function returns (hope that make sense!)
lodash, helps to make the composed functions more readable, because you read in the order the functions will be executed.
composeworks the same as
pipebut with the oposite functions order
- the result of
composeis another function
5. Defining the Broadcaster and Listener Relationship
listeneris just a callback (a function you pass to something else)
broadcasteris a function that takes a
listenerand calls it in different ways
- operators help us to capture better behaviors and "control the future" in a more predictable way.
- when accepting listeners, you don't exactly know when those functions will be executed, maybe they have setTimeouts or setIntervals in them, so eventhough you execute them inmediately in the broadcaster, they may delay the whole exxecution using timeouts.
7. Create a Function to Configure setTimeout
- when using
setTimeout, you can control when and with what is being called, by wrapping it in a function and accept all the parameters you can/need to control
createTimeoutis a function that accepts a time variable, that returns another function that accepts a callback (closing over time), that calls
8. Return a Function to Cancel an Async Behavior
- again, you are wrapping a piece of behaviour with a function, that way, you can control when and where that new function will be called (the cancel function)
9. Wrap addEventListener in a Function for More Control
- You see how
addListeneris chaining functions and getting all the values it needs to create the actual listener? this for me is beautiful!!
10. Create a Utility Function to Control setInterval
- this is very similar to the previous two lessons
- the pattern we are following here is:
- find the pieces you can strip out to control (listener, eventType, time...)
- return a function that can cancel the behavior you just create (clearTimeout or clearInterval)
16. Pass a Done Symbol when an Async Function is Done
- we can take advantage of the iterator protocol and return a "done" value when we reach the last item of our iterables
- you can check if the value passed to the broadcaster is "done" and you can prevent the listeners to continue executing after finish
17. Create an Operator Function to Modify Behaviors
- here you can see the power of functions wrapping other functions!
- we create a new listener that wraps the original and modify the behavior of the original listener by applying some changes before passing the value to it
18. Transform Values with a Map Operator
- operators are functions that accepts a modifier function and change the behavior of our broadcasters and listeners
19. Prevent Certain Values with a Filter Operator
- as you transform before, you can also filter values by passing a
predicatefunction that executes a condition that returns a boolean value
20. Use Pipe to Clean Up Functions Wrapping Functions
- REFACTOR, REFACTOR, REFACTOR!
- compose works here because all the functions inside have the same signature (accept a broadcaster)
- remember that
pipeis the same as
composebut with a readable order of functions
21. Start With the API You Want Then Implement
- start with the API you want and then implement it to meet that API
- you can express what you prefer with your API, either what is really happening with the functions or what is happening in the browser (or in your particular environment)
22. Define a Function to Set Common Behaviors in Operators
- again, the magic of functions wrapping other functions!