JavaScript: Understanding Closures With The Dragon Warrior

A closure is a function having access to the parent scope, even after the parent function has closed. — w3schools

Understanding Function Scope

Every function can access the variables declared in the chain of parent lexical environments.

So, the child functions have access to the parent functions scope. why do we need a term Closures when the term functional scope is descriptive enough? Closures is a behavior exhibited when we deal with the functional scope. Let's see what happens during the last 4 lines.

“A closure is a function having access to the parent scope, even after the parent function has closed”

The “even after ”is the key to understand closures.

Closures In The Valley Of Peace

Even after Master Oogway passed on to the heavens in a stream of peach blossoms, Even after Master Shifu got dismantled by Tai Lung, Po made use of the Closures to access the Dragon scroll (and dumplings) kept safe by Oogway and defeated Tai Lung.

Run the below script here

The Scope Chain And The Execution Stack Of The Characters

Image for post
Image for post
How Po got his access? Through Closures of course.

— The End —

Written by

Full Stack Developer. JavaScript, React, Node, GraphQL.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store