Last active
January 24, 2019 16:16
-
-
Save vivektikar25/9639b515101d0ade2c4352cf2ff6b0b2 to your computer and use it in GitHub Desktop.
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
// =================== Scope ======================= // | |
for (var i = 0; i < 5; i++) { | |
setTimeout(function() { | |
console.log(i); | |
}, 2000); | |
} | |
----------------------------------------------------- | |
for (var i = 0; i < 5; i++) { | |
(function(i) { | |
setTimeout(function() { | |
console.log(i); | |
}, 2000); | |
})(i); | |
} | |
// =================== XXXXXX ======================= // | |
// =================== Closure ======================= // | |
function outerFunction() { | |
var count = 10; | |
return function innerFunction() { | |
console.log(count); | |
}; | |
} | |
var result = outerFunction(); | |
result(); | |
----------------------------------------------------- | |
function outerFunction() { | |
var count = 10; | |
return function innerFunction() { | |
console.log(count); | |
}; | |
} | |
var result = outerFunction(); | |
console.dir(result); | |
----------------------------------------------------- | |
function outerFunction() { | |
var count = 10; | |
return function innerFunction() { | |
console.log("I do nothing"); | |
}; | |
} | |
var result = outerFunction(); | |
console.dir(result); | |
----------------------------------------------------- | |
function foo() { | |
var count = 40; | |
bar(function() { | |
console.log(count); | |
}); | |
} | |
function bar(fun) { | |
console.dir(fun); | |
fun(); | |
} | |
foo(); | |
----------------------------------------------------- | |
function foo() { | |
var count = 40; | |
bar(function() { | |
console.log(count); | |
}); | |
count = 50; | |
} | |
function bar(fun) { | |
console.dir(fun); // Closure scope has value 50; | |
fun(); | |
} | |
foo(); | |
----------------------------------------------------- | |
function foo() { | |
var count = 40; | |
setInterval(function() { | |
count++; | |
}, 3000); | |
setInterval(function() { | |
console.log(count); | |
}, 3000); | |
count = 50; | |
} | |
//Another proof | |
function foo() { | |
var count = 40; | |
setInterval(function() { | |
count++; | |
}, 1000); | |
setInterval(function() { | |
console.log(count); | |
}, 3000); | |
count = 50; | |
} | |
foo(); | |
----------------------------------------------------- | |
var age = 30; | |
function bar() { | |
console.log(age); | |
} | |
function foo() { | |
var age = 20; | |
return bar; | |
} | |
var result = foo(); | |
console.log(result()); // 30 | |
console.dir(result); | |
----------------------------------------------------- | |
var age = 30; | |
function foo() { | |
var age = 20; | |
return function bar() { | |
console.log(age); | |
}; | |
} | |
var result = foo(); | |
console.log(result()); // 20 | |
console.dir(result); | |
// =================== XXXXXX ======================= // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment