Skip to content

Instantly share code, notes, and snippets.

@sanjay1909
Last active August 29, 2015 14:13
Show Gist options
  • Save sanjay1909/90f46ffa9c36308f326a to your computer and use it in GitHub Desktop.
Save sanjay1909/90f46ffa9c36308f326a to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Scope Test</title>
<script>
function print(value, desc) {
var p = document.createElement("p");
p.className = function(value){if(value == true)
return "pass";
else if(value == false)
return "fail";
else
return "null"}(value);
p.appendChild(document.createTextNode(desc));
document.getElementById("results").appendChild(p);
}
</script>
<style>
#results p.pass { color: green; }
#results p.fail { color: red; }
#results p.null { color: black; }
</style>
</head>
<body>
<ul id="results"></ul>
</body>
<script type="text/javascript">
print(null, "-------------- Context: outerFunc --------------- \n");
function outerFunc(){
print( true , innerFunc() + "So here I am, i can be invoked before my declaration ");
function innerFunc(){
return "I am Named function (innerFunc) , [scope] - from begining of outerFunc to its end, "
}
print( true , innerFunc() + "So its straightforward, that i am invoked, after my declaration.")
try{
varAssignedFunc();
}
catch(e){
print(false,"\nvarAssignedFunc - I am not defined to either string, number or function yet, infact i haven't got created yet, you assumed me as function, that why this error, \n" + e + "\n")
}
var varAssignedFunc = function namedFunc(){ // inline function assigned to a variable
return "\tI am function named(namedFunc), assigned to a variable(varAssignedFunc). [scope] - only to the reference(varAssignedFunc). I got invoked through the reference now.";
}
print(true,"varAssignedFunc - I got declared and defined to a function(namedFunc), in outerFunc. [scope] - line i am declared to end of the 'outerFunc.\n", varAssignedFunc());
var num = 5;
if(typeof(num)== "number"){
var description = "\nI am string literal assigned to a var(description) inside the if block{} of outerFunc, its javascript , so i am available outside the if block too";
}
print(true,description);
print(null,"\n\n-------------- Context: Global (window object) --------------- \n");
return "I am Named function(outerFunc), my scope is from begining of Window Function(global context) to its end. "
}
print(true , outerFunc());
try{
globalVariableFunc();
}
catch(e){
print(false , "\nglobalVariableFunc - I am not defined to either string, number or function yet, infact i haven't got created yet, you assumed me as function, that why this error, \n" + e )
}
var globalVariableFunc = function(){
return "\tI am an anonymous function turned as a 'method' to window object. I got assigned to window property: 'globalVariableFunc'. [scope] - only to the reference(globalVariableFunc).I got invoked through the reference(globalVariableFunc) now.";
}
print (true , "globalVariableFunc - I got declared and defined to a anonymous function, in 'window object'(global context). I became a property of window now. [scope] - line i got created to end of the 'Window Function'.\n" + globalVariableFunc() );
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment