Skip to content

Instantly share code, notes, and snippets.

@shoaibmehedi7
Last active April 7, 2021 00:46
Show Gist options
  • Save shoaibmehedi7/0ee714927c39a862ee9b5590003854d4 to your computer and use it in GitHub Desktop.
Save shoaibmehedi7/0ee714927c39a862ee9b5590003854d4 to your computer and use it in GitHub Desktop.
var x = 10;
function createFunction1() {
var x = 20;
return new Function('return x;'); // this |x| refers global |x|
}
function createFunction2() {
var x = 20;
function f() {
return x; // this |x| refers local |x| above
}
return f;
}
var f1 = createFunction1();
console.log(f1()); // 10
var f2 = createFunction2();
console.log(f2()); // 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment