“Penutupan dalam JavaScript” Kode Jawaban

Penutupan ()

// Closures
// In JavaScript, closure is one of the widely discussed and important concepts.
// A closure is a function that has access to the variable from another function’s scope which is accomplished by creating a function inside a function. As defined on MDN:
// “Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure ‘remembers’ the environment in which it was created.”
// In JavaScript, closures are created every time a function is created, at function creation time. Most JavaScript developers use closure consciously or unconsciously — but knowing closure provides better control over the code when using them.
// Example:

function Spellname(name) {
var greet = "Hi, " + name + "!";
var sName = function() {
var welc = greet + " Good Morning!";
console.log(greet);
};
return sName;
}
var Myname = SpellName("Nishi");
Myname();  // Hi, Nishi. Good Morning!

// In the above example, the function sName() is closure; it has its own local scope (with variable welc) and also has access to the outer function’s scope. After the execution of Spellname(), the scope will not be destroyed and the function sName() will still have access to it.

Meandering Meerkat

Penutupan JavaScript

function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12

//my words: what this code do is basically a nested function that will return
//its inner function (un-activated). So var add5 in line 7 activated the outer
//function with 5 as parameter which makes add5 is now the nameless function at
//line 2 that will return 5 + y;

//MDN words:
//add5 and add10 are both closures. They share the same function body
//definition, but store different lexical environments. In add5's lexical
//environment, x is 5, while in the lexical environment for add10, x is 10.
Graceful Grivet

Penutupan dalam JavaScript

//Closures
Closures means a function bind together with its lexical environment
                           	OR
You can say a function along with its lexical scope bundle together forms
a closure
                            OR
In other words, a closure gives you access to an outer function's
scope from an inner function.
//Example
function x(){
  var a = 7;
  function y(){    //function y bind with its lexical enviroment
    console.log(a); 
  }
  a = 100;
  return y;
}
var z = x();
console.log(z) //Output is 100 
Mubashar

Penutupan dalam JavaScript

var counter = (function() {
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val;
  }
  return {
    increment: function() {
      changeBy(1);
    },
    decrement: function() {
      changeBy(-1);
    },
    value: function() {
      return privateCounter;
    }
  };
})();

console.log(counter.value()); // logs 0
counter.increment();
counter.increment();
console.log(counter.value()); // logs 2
counter.decrement();
console.log(counter.value()); // logs 1
Gentle Grouse

Penutupan dalam JavaScript

// Explanation of closure
/* 1 */        function foo()
/* 2 */         {
/* 3 */             var b = 1;
/* 4 */             function inner(){
/* 5 */                 return b;
/* 6 */             }
/* 7 */             return inner;
/* 8 */         }
/* 9 */         var get_func_inner = foo();        
 
/* 10 */         console.log(get_func_inner());
/* 11 */         console.log(get_func_inner());
/* 12 */         console.log(get_func_inner());
Parin Rajpara

Penutupan dalam JavaScript

A closure is a function bundeled together with its lexical scope
Muddy Macaw

Jawaban yang mirip dengan “Penutupan dalam JavaScript”

Pertanyaan yang mirip dengan “Penutupan dalam JavaScript”

Lebih banyak jawaban terkait untuk “Penutupan dalam JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya