re: Explain Closures to me like I am five VIEW POST

FULL DISCUSSION
 

You have a robot kit.

Inside the kit, you an instruction guide and some materials to build the robot, such as screws, metal frames, and a motherboard.

The instruction guide tells you how to use the kit's material to build the robot. But the instruction guide also notes that you need some things that are not included in the kit such as, batteries and a computer, to get the robot up and running.

You follow the kit's instruction guide, use the kit's materials and the not included materials to build a cool robot.

In this case: the closure is the whole robot kit (instruction guide and included materials) + not included materials.

So:

  • The robot kit is the function object (functions are objects in JS)
  • The instruction guide is the function's code that runs when it is invoked
  • The materials included in the kit are the variables/functions declared and used in the function's code
  • The material not included in the kit are variable/functions that are used inside the function's code but was not declared there

So a closure is a function object (code and variables declared in function ) + references variables that are used in the function's code but were declared outside the function

code of conduct - report abuse