Skip to content

Instantly share code, notes, and snippets.

@lastobelus
Created January 21, 2016 16:16
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 lastobelus/6f90eb75b8bef894868d to your computer and use it in GitHub Desktop.
Save lastobelus/6f90eb75b8bef894868d to your computer and use it in GitHub Desktop.
var outerFunction = function(){
if(true){
var x = 5;
//console.log(y); //line 1, ReferenceError: y not defined
}
var nestedFunction = function() {
if(true){
var y = 7;
console.log("2: x is %O", x); //line 2, x will still be known prints 5
}
if(true){
console.log("3: y is %O", y); //line 3, prints 7
}
}
console.log("call nestedFunction before returning it");
nestedFunction();
console.log("change x")
x = 6;
return nestedFunction;
}
var myFunction = outerFunction();
console.log("call the nestedFunction we got");
myFunction();
// playground for observing that this is not lexical
var person = {
name: "Alice",
age: 42,
showInfo: function() {
console.log("1 -- Name:", this.name, " Age:", this.age);
nestedFunction = function() {
console.log("2 -- Name:", this.name, " Age:", this.age);
console.log("2 -- this: %O", this);
}
nestedFunction();
}
}
person.showInfo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment