Skip to content

Instantly share code, notes, and snippets.

View wolfieorama's full-sized avatar
🎯
Focusing

wolfieorama

🎯
Focusing
  • Nairobi, Kigali and else where
View GitHub Profile
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();
function testClosure (){
var x = 4; // local variable
return x;
}
testClosure(); // this will return 4
x; // will return undefined because a function variable are not available once the scope has closed
function testClosure (){
var x = 4;
function closeX(){
return x; // NB: x is not stored anywhere in the innermost function its a global variable
}
return closeX;
}
var checkLocalX = testClosure();