var objLiteral = {
name: 'Kumar'
myNameIs: function() {
return this.name;
}
};
objLiteral.name = 'Arvind';
objLiteral.myNameIs(); // returns Arvind
Can someone explain how the creation phase and execution phase run through this code? I understand how this works for let, const, functions (global execution context, function execution context) but not for object literals.
Here is my take so far:
Creation phase:
objLiteral
object {}
is placed somewhere in memory and is 'live and accessible' and not garbage collected until the end of the program.name
with a pointer returned back to the object so that it can be found during execution.myNameIs
with a pointer returned back to the object so that it can be found during execution.Execution phase:
objLiteral.name
is set to the value 'Arvind'
, by tracing the memory locations previously stored.objLiteral.myNameIs()
is invoked and using the memory location of the object and the pointer to the method/myNameIs
a 'function execution context' is created. The value of this
is taken from the object the method is called on. Therefore this==objLiteral
this
is the same as objLiteral
the memory trace to the location of the value is the same as objLiteral.name
and it returns 'Arvind'
When is the variable objLiteral
assigned a value i.e. given a reference to the object? During the creation phase or execution phase?
Here is my take so far. Creation phase: the
objLiteral
object{}
is placed somewhere in memory and is 'live and accessible' and not garbage collected until the end of the program.
No, absolutely not. An execution context, and the separation between creation phase and execution phase, has nothing to do with object (literal) creation. It is solely about code execution, whether that is in the global script scope or in a function call. An execution context basically is a stack frame.
Objects from object literals are allocated, created and mutated during the execution phase, never during the creation phase1.
So what happens in your example is:
objLiteral
is allocated and initialised with undefined
an object is created, a string Kumar
is created and placed in its .name
property, a function (with a closure over the current scope) is created and placed in its .myNameIs
property, the object is assigned to the objLiteral
variable
the object in the variable objLiteral
is looked up, a string Arvind
is created assigned to the object's .name
property
the object in the variable objLiteral
is looked up, its .myNameIs
property is looked up, and is invoked as a method. This creates a new execution context:
this
value is set to the method call receiver, i.e. the objectthis
is looked up and its .name
property is accessedreturn
ed, ending the function executionThe execution context of the function ends, its local scope is no longer referenced and can be garbage-collected, the execution of the global script resumes with the returned string value.
Garbage collection has nothing to do with this either. Objects are garbage-collected as soon as they are no longer referenced from anything (variables or properties), variables (and scopes) are garbage-collected as soon as they are no longer referenced from anything (live stack frames or closures).
1: I wrote "from object literals" specifically, as some function objects are created during the creation phase from function
declarations