Mastering JavaScript πŸ§‘β€πŸ’»: Closures

Hello everyone πŸ‘‹, I hope you are doing well.

In this post I will explain what closures are using examples as well so that you too can use them to write your code better.

LEXICAL SCOPE ✍️

Before directly explaining what closures are, it is …


This content originally appeared on DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» and was authored by Saverio683

Hello everyone πŸ‘‹, I hope you are doing well.

In this post I will explain what closures are using examples as well so that you too can use them to write your code better.

LEXICAL SCOPE ✍️

Before directly explaining what closures are, it is appropriate to explain the concept of scope and lexical scope.

The scope of a function refers that area of code where its variables are "visible" and can be used.
If another function is created within one function, another internal scope will then be created, and so on: this is called a scope chain.

Lexical scope, also called static scope, refers to when the location of a function's definition determines which variables you have access to.

E.g. :

const life = () => {
  const pastEvents = 'past'

  //console.log(actualEvents) this will create an error because that inner variable is not accesible here

  const present = () => {    
    const actualEvents = 'present'

    console.log(pastEvents, actualEvents)//ok
    //console.log(futureEvents) same for this one

    const future = () => {
      const futureEvents = 'future'

      console.log(pastEvents, actualEvents, futureEvents)//ok      
    }

    future()
  }

  present()
}

life()
/*
Outputs:
  past present
  past present future
*/

As you can see, in a nested group of functions the inner functions have access to the variables and other resources of their parent scope.

CLOSURES πŸͺ†

Let's make some changes to the code from before:

const life = () => {
  const pastEvents = 'past'

  const present = () => {    
    const actualEvents = 'present'

    console.log(pastEvents, actualEvents)

    const future = () => {
      const futureEvents = 'future'

      console.log(pastEvents, actualEvents, futureEvents)  
    }

    return future
  }

  return present
}

const newFunc = () => {
  //different scope
  const newLife = life()
  const future = newLife()

  future() 
}

newFunc()
/*
Outputs:
  past present
  past present future
*/

New functions created within the newFunc function have access to the variables pastEvents, actualEvents and futureEvents even if they are in another scope.
This is because the future and present functions are closures: when they are assigned to variables (in our case newLife and future) they close over (from here the term closure) the variables of their lexical scope.

Due to this the closures are memory efficient saving code repetitions.
Here another example:

const addTo = x => y => x + y //x = outer func; y = inner.
const addToTen = addTo(10)
addToTen(3) // returns 13

Another benefit they bring is that of data encapsulation:

const createCounter = () => {
  let count = 0

  return {
    increment() {
      count++
    },
    decrement() {
      count--
    },
    showCount() {
      console.log(count)
    }
  }
}

const myCounter = createCounter()

for(let i = 0; i < 100; i++)
  myCounter.increment()

myCounter.decrement()

console.log(myCounter.count)//returns undefined
myCounter.showCount()//returns 99

In this way, the count variable is private and can be accessed only through proper functions written by the developer.

CONCLUSIONS

If you have come this far in reading, I thank you. If you liked the post please leave a like and check out my other posts πŸ™‚.


This content originally appeared on DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» and was authored by Saverio683


Print Share Comment Cite Upload Translate Updates
APA

Saverio683 | Sciencx (2022-09-10T07:35:51+00:00) Mastering JavaScript πŸ§‘β€πŸ’»: Closures. Retrieved from https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/

MLA
" » Mastering JavaScript πŸ§‘β€πŸ’»: Closures." Saverio683 | Sciencx - Saturday September 10, 2022, https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/
HARVARD
Saverio683 | Sciencx Saturday September 10, 2022 » Mastering JavaScript πŸ§‘β€πŸ’»: Closures., viewed ,<https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/>
VANCOUVER
Saverio683 | Sciencx - » Mastering JavaScript πŸ§‘β€πŸ’»: Closures. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/
CHICAGO
" » Mastering JavaScript πŸ§‘β€πŸ’»: Closures." Saverio683 | Sciencx - Accessed . https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/
IEEE
" » Mastering JavaScript πŸ§‘β€πŸ’»: Closures." Saverio683 | Sciencx [Online]. Available: https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/. [Accessed: ]
rf:citation
» Mastering JavaScript πŸ§‘β€πŸ’»: Closures | Saverio683 | Sciencx | https://www.scien.cx/2022/09/10/mastering-javascript-%f0%9f%a7%91%f0%9f%92%bb-closures/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.