Lädt...


🔧 JavaScript Closures Cheatsheet


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

1. What is a Closure?

A closure is a function that retains access to its lexical scope even when the function is executed outside that scope.

2. Basic Example

function outerFunction() {
    let outerVariable = 'I am outside!';

    function innerFunction() {
        console.log(outerVariable);
    }

    return innerFunction;
}

const myFunction = outerFunction();
myFunction(); // Output: I am outside!

3. Lexical Scoping

Closures work by capturing variables from their lexical environment.

function makeCounter() {
    let count = 0;

    return function() {
        count++;
        return count;
    }
}

const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2

4. Immediately Invoked Function Expression (IIFE)

IIFEs create closures to encapsulate variables.

const increment = (function() {
    let counter = 0;
    return function() {
        counter++;
        return counter;
    }
})();

console.log(increment()); // 1
console.log(increment()); // 2

5. Closure with Loop (Common Pitfall)

Using var in a loop can lead to unexpected behavior due to function-level scope.

for (var i = 0; i < 3; i++) {
    setTimeout(function() {
        console.log(i);
    }, 1000);
}
// Output: 3, 3, 3

Solution with let:

for (let i = 0; i < 3; i++) {
    setTimeout(function() {
        console.log(i);
    }, 1000);
}
// Output: 0, 1, 2

6. Data Privacy

Closures can be used to emulate private variables.

function createSecretHolder(secret) {
    let secretValue = secret;

    return {
        getSecret: function() {
            return secretValue;
        },
        setSecret: function(newSecret) {
            secretValue = newSecret;
        }
    };
}

const holder = createSecretHolder('mySecret');
console.log(holder.getSecret()); // 'mySecret'
holder.setSecret('newSecret');
console.log(holder.getSecret()); // 'newSecret'

7. Partial Application

Closures can be used for partial function application.

function add(a) {
    return function(b) {
        return a + b;
    }
}

const addFive = add(5);
console.log(addFive(3)); // 8

8. Function Factories

Closures enable function factories.

function multiplier(factor) {
    return function(number) {
        return number * factor;
    }
}

const double = multiplier(2);
console.log(double(5)); // 10

const triple = multiplier(3);
console.log(triple(5)); // 15

9. Module Pattern

The module pattern uses closures to create public and private methods.

const Module = (function() {
    let privateVar = 'I am private';

    function privateMethod() {
        console.log(privateVar);
    }

    return {
        publicMethod: function() {
            privateMethod();
        }
    };
})();

Module.publicMethod(); // 'I am private'

10. Handling Asynchronous Closures

function createCounter() {
    let count = 0;

    return {
        increment: function() {
            count++;
            return count;
        },
        getCount: function() {
            return count;
        }
    };
}

const asyncCounter = createCounter();

setTimeout(() => {
    asyncCounter.increment();
    console.log(asyncCounter.getCount()); // 1
}, 1000);

11. Closures in Event Listeners

function setupButton() {
    let clickCount = 0;

    document.getElementById('myButton').addEventListener('click', function() {
        clickCount++;
        console.log(`Button clicked ${clickCount} times`);
    });
}

setupButton();

12. Common Use Cases

  • Encapsulation: Protecting variables from being accessed or modified outside their intended context.
  • Callbacks: Passing a function as an argument to be executed later.
  • Event Handlers: Managing event-driven programming.
  • Functional Programming: Creating higher-order functions and function factories.

13. Best Practices

  • Minimize the use of closures for better memory management.
  • Be mindful of scope and lifetime of variables.
  • Use const or let instead of var to avoid common pitfalls with closures in loops.

This cheatsheet provides a comprehensive overview of closures in JavaScript, making it ideal for quick revision and reference.

...

🔧 JavaScript Closures Cheatsheet


📈 44.4 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 26.88 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Closures: A Comprehensive Guide


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Beginner's Guide


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 26.88 Punkte
🔧 Programmierung

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


📈 26.88 Punkte
🔧 Programmierung

🔧 JavaScript Closures


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures ⚡️


📈 26.88 Punkte
🔧 Programmierung

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


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Comprehensive Guide


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Deep Dive


📈 26.88 Punkte
🔧 Programmierung

🔧 Exploring JavaScript Closures: Practical Examples and Insights


📈 26.88 Punkte
🔧 Programmierung

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


📈 26.88 Punkte
🔧 Programmierung

🔧 Mastering Closures: Tips and Tricks for Better JavaScript Development


📈 26.88 Punkte
🔧 Programmierung

🔧 JavaScript Closures


📈 26.88 Punkte
🔧 Programmierung

🔧 How Do Closures Work in JavaScript? Explained with Code Examples


📈 26.88 Punkte
🔧 Programmierung

🔧 USING JAVASCRIPT CLOSURES IN REACT


📈 26.88 Punkte
🔧 Programmierung

🔧 JavaScript Closures in Action: Real-World Applications


📈 26.88 Punkte
🔧 Programmierung

🔧 Let's Understand JavaScript Closures: A Fundamental Concept


📈 26.88 Punkte
🔧 Programmierung

🔧 A Practical Introduction to Closures in JavaScript: Part 1


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Powerful Mechanism for Variable Scope


📈 26.88 Punkte
🔧 Programmierung

🔧 JavaScript Closures: Demystified


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures: A Comprehensive Guide


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures


📈 26.88 Punkte
🔧 Programmierung

🔧 Warping realities: JavaScript closures in the Developer's Toolkit.


📈 26.88 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Closures ⚡️


📈 26.88 Punkte
🔧 Programmierung

🔧 🚀 JavaScript Functions: Arrow Functions, Callbacks, and Closures 📜


📈 26.88 Punkte
🔧 Programmierung

🔧 📦🔓Closures in JavaScript decoded


📈 26.88 Punkte
🔧 Programmierung

🔧 Closures, Higher-Order Functions, and Prototypal Inheritance in JavaScript


📈 26.88 Punkte
🔧 Programmierung

🔧 Closures in JavaScript


📈 26.88 Punkte
🔧 Programmierung

🎥 JavaScript Interview Prep: Functions, Closures, Currying


📈 26.88 Punkte
🎥 Video | Youtube

🔧 Scope, Closures, and Hoisting in JavaScript – Explained with Code Examples


📈 26.88 Punkte
🔧 Programmierung

🔧 [JavaScript] Understand closures in 111 seconds


📈 26.88 Punkte
🔧 Programmierung

matomo