DEV Community

Discussion on: Explain Closures to me like I am five

Collapse
 
loujaybee profile image
Lou (🚀 Open Up The Cloud ☁️) • Edited

You are born in a room.

For the rest of your life you're only allowed to eat foods that were in the room you were born in.

If there's a window in your room and there are foods in the room behind the window, you can eat those too.

You can try to eat other foods but you'll get told-off if you do (oh, and you won't get the food, no matter how hard you try)

  • Birth is function definition / creation.
  • Food is variables (reference to values)
  • Rooms are functions (and their enclosing scope (food))
  • Windows are reference to other functions in scope.