Created
April 29, 2016 18:51
-
-
Save kocubinski/09442f5ca030d36a9f3a7dab183f9ed1 to your computer and use it in GitHub Desktop.
Javascript questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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