Questions tagged [lexical-closures]

Lexical closures are functions, often anonymous, that capture, or close over, their lexical environment. Lexical closures are used extensively in functional programming techniques that involve higher-order functions.

53 questions
0
votes
2 answers

Dart. Where argument of anonymous function came from?

In example below there (num i) stuff, where it get it's value when makeAddr() called ? Function makeAdder(num addBy) { return (num i) => addBy + i; } void main() { // Create a function that adds 2. var add2 = makeAdder(2); // Create a…
Jack J
  • 189
  • 2
  • 2
  • 8
0
votes
3 answers

How does the Javascript closure with function works?

Hi i have been exploring closures and javascript core concepts i couldn't understand why does the console.log(factory[i]) outputs undefined i have pushed my function inside there right? And if i call temp outside the loop it says undefined whereas…
Kannan T
  • 1,639
  • 5
  • 18
  • 29
0
votes
0 answers

lexical scoping example not working

Why would this closure not work? var derivedKey; lightwallet.keystore.deriveKeyFromPassword(password, function (err, pwDerivedKey) { derivedKey = pwDerivedKey; }); if (!derivedKey){ console.error ("ERROR no derivedKey"); } Thows…
Roland Kofler
  • 1,332
  • 1
  • 16
  • 33
0
votes
3 answers

Javascript Lexical scope

I am trying to understand the concept of Lexical scope. As far as i know Lexical scope does not work backwards. In the below javascript code i have declared variable 'name' in scope3() function. But i tried to call it in scope1() and scope2()…
SsNewbie
  • 257
  • 2
  • 8
  • 21
0
votes
3 answers

Does using lambda functions to simulate lexical clousures have unforseen performace / implementation issues?

Program 1: #include std::string Hello(void){return "Hello";} std::string World(void){return "world!";} int main(){ std::cout << Hello() << " " << World() << std::endl; } The functions Hello() and World() exist in the global…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
-1
votes
3 answers

Is it possible to differentiate between a non-capturing function and a closure in Javascript?

The two function objects: // toplevel var f1 = function(){return k;}; var f2 = (function(k){return function(){return k;}})(42); have the same source code "function(){return k;}" but f1 is a function where k is looked up in the global environment,…
6502
  • 112,025
  • 15
  • 165
  • 265
-2
votes
2 answers

Difference between closures and normal functions

I was going through the closures definition in Mozilla and found this example - function makeSizer(size) { return function () { document.body.style.fontSize = `${size}px`; }; } const size12 = makeSizer(12); const size14 =…
beta programmers
  • 513
  • 2
  • 8
  • 23
-2
votes
1 answer

Code with var variable at each level gives unexpected result

Please provide an explanation why the output will be 4. I am trying to understand that the output is 4 but cannot find the reason why is it not 3. var x=4, obj={ x: 3, bar:function(){ var x = 2; setTimeout(function(){ …
1 2 3
4