Tetralogy:

  • Start by creating an Activation Object (AO) execution context

  • Find the parameter and variable declarations and use the variable and parameter names as AO attribute names with the value undefined

    (The second step is also called variable declaration promotion)

  • Unify argument values and parameters

  • Find the function declaration in the function body and assign the value to the function body

Do an experiment of your own, for example:

function fn(a) {
    console.log(a);
    var a = 123;
    console.log(a);
    function a () {}
    console.log(a);
    var b  = function () {};
    console.log(b);
}
fn(1);
Copy the code

Resolution:

OA{a: undefined; OA{a: undefined; OA{a: undefined; b: undefined; } // Step 3: unify the parameter OA{a:1; b:undefined; Function (){OA{a: function a (){}; function(){a: function a (){}; b: undefined; } Function a() {} function a() {} function a() {} function a() {} A is assigned to 123 again so the second console.log(a) prints 123 if you look down, Function a () {} has been promoted during precompilation, so the third console.log(a) still prints 123. If you look down, b is reassigned. Function b () {} so OA{a: function(){}; b: function(){}; } so fourth console.log(b); Function (){}Copy the code