Lädt...

🔧 A Practical Introduction to Closures in JavaScript: Part 1


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Closures are a very interesting topic, and it is a demonstration of how to leverage the lexical scope in JavaScript.

Let's start by showing a code example first:

function outerFunction() {
  let count = 0;

  return function innerFunction() {
   count += 1;
   console.log(`Count is now ${count}`)
  }
}

Before adding more code, I think it is important to mention that when a function is invoked, after doing its stuff and returning its output, all the variables and functions and any data inside that function will be completely gone.

So, one might think that, if that's the case, the count variable will dissapear right after outerFunction is invoked, and the returned function will lose access to the count variable.

But, due to the lexical nature of JavaScript, when a function is created, like innerFunction in this case, it will always be returned along with its scope.

Even after the outerFunction has finished executing, the innerFunction retains access to the variables in its outer scope, allowing it to keep data private and alive throughout the lifecycle of the application.

Let's continue with our example above:

function outerFunction() {
  let count = 0;

  return function innerFunction() {
   count += 1;
   console.log(`Count is now ${count}`)
  }
}

const myFunction = outerFunction() // outerFunction is executed and, and myFunction becomes innerFunction

myFunction() // Count is now 1
myFunction() // Count is now 2
myFunction() // Count is now 3

So basically functions can carry with them the scope in which they were created. This includes any variables or fucntions that were in scope at the time of creation.

So as you can see, when we return innerFunction from outerFunction, we're not just returning the innerFunction code, we're returning a package of the function and its scope, and this scope that gets returned along with the function, and to which the function keeps access to, is what we call a closure.

Long story short: in JavaScript, a closure is created every time a function is created.

Comming soon...

I will continue expanding on this topic soon, by deepen the discussion on lexical scoping and practical applications like memoization.

...

🔧 A Practical Introduction to Closures in JavaScript: Part 1


📈 45.29 Punkte
🔧 Programmierung

🔧 Mastering Closures in JavaScript: A Practical Real-World Example 💡


📈 31.84 Punkte
🔧 Programmierung

🔧 Exploring JavaScript Closures: Practical Examples and Insights


📈 31.84 Punkte
🔧 Programmierung

🔧 The Ultimate Guide to iOS Development: Closures (Part 7)


📈 22.34 Punkte
🔧 Programmierung

🔧 JavaScript Closures: They're Not as Scary as You Think!


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: From Confusion to Clarity


📈 22.04 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Closures: A Comprehensive Guide


📈 22.04 Punkte
🔧 Programmierung

🔧 Explore "Closures" - A powerful important feature in JavaScript


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Beginner's Guide


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures with Real-World Examples


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 22.04 Punkte
🔧 Programmierung

🔧 JavaScript Closures: A Beginner's Guide


📈 22.04 Punkte
🔧 Programmierung

🔧 JavaScript Closures


📈 22.04 Punkte
🔧 Programmierung

🔧 Demystifying JavaScript Closures: A Deep Dive with Examples


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures in 10 Minutes


📈 22.04 Punkte
🔧 Programmierung

🔧 Closures in JavaScript: What They Are and Why They Matter


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures ⚡️


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures (Without the Headache) 🤯


📈 22.04 Punkte
🔧 Programmierung

🔧 What Are Closures in JavaScript? (1 Minute Guide)


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 22.04 Punkte
🔧 Programmierung

🔧 The Magic of JavaScript Closures: A Clear and Easy Guide


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Comprehensive Guide


📈 22.04 Punkte
🔧 Programmierung

🔧 JavaScript Closures Explained with a Crazy Grandfather’s Treasure 🏴‍☠️💰


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Variable Access in JavaScript: Scope, Hoisting, and Closures


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Core JavaScript Concepts: Objects, Scopes, and Closures


📈 22.04 Punkte
🔧 Programmierung

🔧 Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes


📈 22.04 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Deep Dive


📈 22.04 Punkte
🔧 Programmierung

🔧 JavaScript Closures Explained Like You’re Five


📈 22.04 Punkte
🔧 Programmierung

🔧 Mastering Closures in JavaScript: A Complete Guide with Examples


📈 22.04 Punkte
🔧 Programmierung

🔧 JavaScript Closures Cheatsheet


📈 22.04 Punkte
🔧 Programmierung

🔧 Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes


📈 22.04 Punkte
🔧 Programmierung

🔧 Deep Dive into JavaScript Closures: How and When to Use Them


📈 22.04 Punkte
🔧 Programmierung

matomo