Skip to content

Instantly share code, notes, and snippets.

@kocubinski
Created April 29, 2016 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kocubinski/09442f5ca030d36a9f3a7dab183f9ed1 to your computer and use it in GitHub Desktop.
Save kocubinski/09442f5ca030d36a9f3a7dab183f9ed1 to your computer and use it in GitHub Desktop.
Javascript questions
// Function closures:
var fn = function(x) {
return function() { return x = x + 1; };
};
var fn2 = fn(2);
// Q. What is stored in the variable fn2?
// A. A function which, when called, increments x by 1 and returns the result.
// A common mistake is to think it returns either 2, or 3.
// Q. What is the return value of running:
fn2(); // => 3
fn2(); // => 4
fn2(); // => 5
// They should know at least that fn(2) returns a function.
// If they are fuzzy on how x maintains state (keeps increasing), that's OK, but not great.
// common js idioms:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment