If we have the following code block, when we call obj.getX()
, which step does this correspond to according to the spec?
How does the js engine evaluate obj.getX()
when it comes to setting the this
value to the context object on the left hand side?
const obj = {
x: 1,
getX() {
return x
}
}
The spec states in EvaluateCall
13.3.6.2 EvaluateCall ( func, ref, arguments, tailPosition )
The abstract operation EvaluateCall takes arguments func (an ECMAScript language value), ref (an ECMAScript language value or a Reference Record), arguments (a Parse Node), and tailPosition (a Boolean) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It performs the following steps when called:
1. If ref is a Reference Record, then a. If IsPropertyReference(ref) is true, then i. Let thisValue be GetThisValue(ref). b. Else, i. Let refEnv be ref.[[Base]]. ii. Assert: refEnv is an Environment Record. iii. Let thisValue be refEnv.WithBaseObject(). 2. Else, a. Let thisValue be undefined. 3. Let argList be ? ArgumentListEvaluation of arguments. 4. If func is not an Object, throw a TypeError exception. 5. If IsCallable(func) is false, throw a TypeError exception. 6. If tailPosition is true, perform PrepareForTailCall(). 7. Return ? Call(func, thisValue, argList).