UNDERSTAND JAVASCRIPT FUNCTIONS IN DEPTH - JavaScript Fundamentals.

Поделиться
HTML-код
  • Опубликовано: 13 окт 2024

Комментарии • 1

  • @devafterdark
    @devafterdark Год назад

    didn't mention closure scope, it allows you to have private member variables in javascript:
    const Counter = () => {
    let count = 0;
    return {
    incrementCount: () => count++,
    getCount: () => count
    }
    }
    const counter = Counter();
    console.log(counter.getCount()); // 0
    counter.incrementCount();
    console.log(counter.getCount()); // 1
    the count member variable of the function Counter is not accessible outside of the function scope, but it's value is still kept in memory because the object returned from counter still references it, and is the only way to access count. The garbage collector does NOT free resources that are still referenced. proper OOP hehe