I am interested in how the compiler resolves this reference, on the internals. Say we have this code :
function makeConsumer(run_priority,run_object) {
var consumer = function(last_run_action) {
if(!!last_run_action) {
// do something
} else {
var next_to_load = run_priority.shift();
next_to_load.consume = consumer;
run_load_script(next_to_load,run_object);
}
};
return consumer;
}
There is a reference to consumer
at the top, and then, before the function has finished being defined, another reference to consumer
occurs in the final else
block. How is this reference to consumer valid if the original consumer has not even been assigned at the time when the code is execute? I understand that function pushes scope to the top, but is this also true for a function expression assigned to a variable?
Is it possible to create a scenario in which a function references itself via a variable it is assigned to, and that reference is not yet valid? How does javascript make the second reference to consumer
refer to the function, when that reference is part of the function that has not even finished being defined?
Is this equivalent to the references used in recursion? And if so, how are they evaluated by the compiler to be valid?