Tuesday, December 27, 2022

Function.prototype.call

Function.prototype.call ( thisArgument, argumentsList )

Example

function anInstanceOfFunction(data)
{
    this.data = data;
}

thisArgument = {"data" : null};

// all instances of Function(function definitions or dynamical functions by `new Function`) has
// their [[prototype]] chained to the Function constructor's prototype property(Function.prototype)
anInstanceOfFunction.call(thisArgument, "dat");

console.log(thisArgument.data);

Function.prototype.call ( thisArgument, argumentsList )

note Function.prototype itself is a function object(to ensure compatibility with es2015), which returns undefined when called; but the call here is `Function.prototype.` instead of `Function.prototype(`

  1. If IsCallable(F) is false, throw a TypeError exception.//F is anInstanceOfFunction in our example
  2. Pop from the execution context stack//the top execution context has no further use and optimized out
  3. Let calleeContext be PrepareForOrdinaryCall(F, undefined):
            calleeContext = {
                "Function" : F,
                "LexicalEnvironment" : new NewFunctionEnvironment(F),
            }
            calleeContext.VariableEnvironment = calleeContext.LexicalEnvironment
            Push calleeContext onto the execution context stack
            return calleeContext
        
    
  4. calleeContext.LexicalEnvironment.EnvironmentRecord.[[ThisValue]] = thisArgument
  5. Let result be Evaluate F with argumentsList.
  6. Remove(exactly, Pop) calleeContext from the execution context stack
  7. If result.[[Type]] is return, return NormalCompletion(result.[[Value]]).
  8. ReturnIfAbrupt(result).
  9. Return NormalCompletion(undefined).

No comments:

Post a Comment