Skip to content

Instantly share code, notes, and snippets.

@sivaprabug
Created December 21, 2015 03:18
Show Gist options
  • Select an option

  • Save sivaprabug/c3cc794086c31c7a8067 to your computer and use it in GitHub Desktop.

Select an option

Save sivaprabug/c3cc794086c31c7a8067 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Scope Chain</title>
</head>
<p>
<ul>
<li>When you have nested functions you can create a scope 'Chain'</li>
<li>The browser travels up this chain when looking for a variables value</li>
</ul>
</p>
<body>
<script>
var myVar = "A global Variable";
function grandParent() {
var myVar = "A Variable local to grandParent()";
console.log(myVar);
function parent() {
var myVar = "A Variable local to parent()";
console.log(myVar);
function child() {
var myVar = "A Variable local to child()";
console.log(myVar);
}
child();
window.child = child;
}
parent();
window.parent = parent;
}
grandParent();
window.grandParent = grandParent;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment