Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Last active December 14, 2015 17:49
Show Gist options
  • Save StarpTech/5124624 to your computer and use it in GitHub Desktop.
Save StarpTech/5124624 to your computer and use it in GitHub Desktop.
Javascript execution-context

#Javascript execution-context

####References Name_conflicts

Really nice documentation about the execution context in JavaScript

###Activation Object

  • Before executing any function code the execution context is created.
    1. The Variable Object is created with the function arguments, inner variables and function declaration.
    2. The ScopeChain is created with all data of the VariableObject and all parent execution contexts (VariableObjects).

##For this example (Creation stage):

####[Global]

  • Function declarations:
    • b()
  • Variables:
    • a

####[Function b]

  • Function declarations:
    • a()
  • Variables:
    • a

##Why is a equals 2 and not equals 10 ?

The reason is easy function declaration are scanned at first by Javascript so the property a is created in the variable object of the [function object b] which contain the function declaration function a(){};. After this process the variables are scanned and javascript found already a property with the same name in the variable object of the function and the behaviour of already found variable declarations is do nothing and continue scanning. This behavior results in that the variable assignment of a = 10 is hidden for the outer scope. The return has no effect in the context of the variable object but in the execution stage!

var a = 2;

function b() {

console.log(typeof a);  //function

console.log(a); //prints [Function: a]

a = 10;

console.log(typeof a); //Number
return; //This return has no effect in the context variable object but in the execution stage!
function a() {} 


}

b();
console.log(a);



//Example 2
var d = function(){

  console.log(e); //[Function: e]
	console.log(f); //undefined
	var e = 50;
	function e(){};
	var f = function(){}; //Function declaration?? No that is a variable declaration which are initialized with 'undefined'.
	

};

d();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment