Determining the this
binding for an executing function requires finding the direct call-site of that function. Once examined, four rules can be applied to the call-site, in this order of precedence:
Called with new
? Use the newly constructed object.
Called with call
or apply
(or bind
)? Use the specified object.
Called with a context object owning the call? Use that context object.
Default: undefined
in strict mode
, global object otherwise.
Be careful of accidental/unintentional invoking of the default binding rule. In cases where you want to "safely" ignore a this
binding, a "DMZ" object like ø = Object.create(null)
is a good placeholder value that protects the global
object from unintended side-effects.
Instead of the four standard binding rules, ES6 arrow-functions use lexical scoping for this
binding, which means they adopt the this
binding (whatever it is) from its enclosing function call. They are essentially a syntactic replacement of self = this
in pre-ES6 coding.