My question arose while I was trying to figure out all the details of this
keyword. So far, I understood that how this value is set by reading ECMAScript® Language Specification Section 10.4.3 Entering Function Code and 11.2.3 Function Call. And while doing playing with some code, my question arose, please see the code below.
"use strict";
function test(){
alert(this); //undefined
};
test();
According to 10.4.3 Step 1, given thisArg
is assigned to this
value in strict mode
. And according to 11.2.3 Step 7, thisArg is only undefined when Type(ref)
is not a reference
.
So my question is, why is Type(ref)
not a reference
in this case?
Step 7 is irrelevant in this case because it is the else after step 6, which says:
6.If Type(ref) is Reference, then
a.If IsPropertyReference(ref) is true, then
i.Let thisValue be GetBase(ref).
b.Else, the base of ref is an Environment Record
i.Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).
In your case test
is a Reference, and it is not a PropertyReference, so the ImplicitThisValue is used. In strict mode, that value is undefined
.