What happens when you run java script programm?
As you know when you run the java script program everything will happen inside the Execution Context as we discussed in the article. Java script execution is not possible without the execution context. When you run a java script program the execution context will be created. Suppose we have following line of code to execute like.
var n=2;
function cube(number)
{
var result=number*number*number;
return result;
}
var cubeof4=cube(4);
var cubeof6=cube(6);
When you run this program a global execution context will be created.
The execution context will be created in two phases.
1) Creation Phase Or Memory Creation Phase.
2) Code Execution Phase.
What will happen in the First Phase/Creation Phase/Memory Execution Phase?
- It will allocate the memory to all the variables and allocate a special value 'undefined'.
- It will allocate the memory to the function 'cube'.
- You can understand it with the help of give diagram.
- When a new function will be invoked a new Execution Context will be created like from lines.
- var cubeof4=cube(4);
- var cubeof6=cube(6);
function cube(number)
{
var result=number*number*number;
return result;
}
The memory will be allocated to variables and functions inside the function.
Note:-return control states that return the control to the place where the function was invoked.
You can understand from this diagram
After function invocation the Execution Context will be deleted.
Note:-For the second invocation the again new Execution Context will be created.
Java Script Engine handle everything to manage this execution context creation, deletion and control.
It manages a stack that is known as call stack. Java Script has its own call stack.
Call stack:-Just like a stack and at very bottom we will always have Execution Context like.
When a new execution context will be created so there will be entry in the call stack.Suppose E1 and E2 is the Execution Context after execution of this it will be moved out from the stack and the control will again come to the Global Execution Context.
When all things will be executed the call stack will be empty.
Call Stack is also known as
- call stack.
- Execution Context Stack.
- Program Stack.
- Control Stack.
- Runtime Stack.
- Machin Stack.
Comments
Post a Comment