Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

closure

function outer() {
  const outerVar = "Hi i am outerVar";
  function inner() {
    const innerVar = "Hi i am innerVar";
    console.log(outerVar);
    console.log(innerVar);
  }
  return inner;
}

const innerFn = outer();
innerFn(); 
// Hi i am outerVar
// Hi i am innerVar
Comment

closure

function makeFunction() {
  const name = 'TK';
  function displayName() {
    console.log(name);
  }
  return displayName;
};
Comment

closure

var counter = (function() {  //exposed function references private state (the outer function’s scope / lexical environment) after outer returns. 
 var privateCounter = 0;
 function changeBy(val)   { privateCounter += val; }
 return {  increment: function() {changeBy(1); },
           decrement: function() {changeBy(-1);},
           value: function() {return privateCounter; }
  };
})();

counter.increment(); counter.increment();
counter.decrement();
counter.value();      //  1
Comment

closures

// Closures
//Almost mystical like feature that many developers fail to fully understand.
//We cannot create closures manually like how we create arrays and functions.
//Rather closures happen in certain  situations. We just need to recognize 
//those situations.

//Example:
const secureBooking = function () {
   let passengerCount = 0; //Variable of the secureBooking Function
   
  //returns a function
   return function () {
     passengerCount++; //Adding to the passengerCount
     console.log(passengerCount);
  };
 };

const book = secureBooking(); //capture that function
book();

//In the above example we have a function called secureBooking
//That function returns another function, which we stored in book variable
//We then call the book() function and it adds to the passengerCount.
//But you may be wondering? How can it add to the passengerCount
//if the secureBooking has finished executing, shouldn't it not exist?
//This works because all functions have access to the variable environment
// in which they were created in. Meaning since secureBooking created
// the function which we stored in book. The book function now has 
// access to the variable environment of secureBooking function.
Comment

what is closure

outer = function() {
  var a = 1;
  var inner = function() {
    console.log(a);
  }
  return inner; // this returns a function
}

var fnc = outer(); // execute outer to get inner 
fnc();
Comment

closure

func main() {
  x := 0
  increment := func() int {
    x++
    return x
  }
  fmt.Println(increment())
  fmt.Println(increment())
}
Comment

closure

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

 let addTen = add(10);
 let addSeven = addTen(7);

 console.log(addSeven); // 17
Comment

closure

def say():
    greeting = 'Hello'

    def display():
        print(greeting)

    display()
Code language: Python (python)
Comment

PREVIOUS NEXT
Code Example
Javascript :: content uri react native fs 
Javascript :: is an Angular component, then verify that it is part of this module. 
Javascript :: angular chartjs align legend left 
Javascript :: how to print 1 to 10 table in javascript 
Javascript :: alpine js x-on click not working 
Javascript :: filter array js 
Javascript :: sum array elements in javascript 
Javascript :: environment variable to debug knex 
Javascript :: discord js invite to channel 
Javascript :: ngif react 
Javascript :: jquery datepicker enable year selection 
Javascript :: send data with emit angular 
Javascript :: react switch case 
Javascript :: codemirror get object from textarea 
Javascript :: where to initialize state in react 
Javascript :: js alert with multiple buttons 
Javascript :: array last element 
Javascript :: regex check for anchor tag with specific text 
Javascript :: convert Component Did mount into useEffect 
Javascript :: joining array of string 
Javascript :: what hostings can run react js 
Javascript :: javascript get the last item in an array 
Javascript :: react clone element 
Javascript :: javascript cheatsheet 
Javascript :: Get title assert 
Javascript :: react split 
Javascript :: js arrow anonymous function 
Javascript :: materal ui react range slider 
Javascript :: js ismobile detected 
Javascript :: react footer component 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =