thisis the runtime context of afunction.thisis determined by the call site- the same function can be executed with different
thisruntime contexts. You can think ofthisas another arguement to the function - Comparison: scopes are generally defined at compile time (exception:
eval)
const person = {
name: 'Alex',
getName() {
return this.name;
}
}
const foo = person.getName;
// using default binding (`undefined` in strict mode)
foo();
// using explicit binding
person.getName.call({name: 'Ben'});
// using implicit binding
// using `person` as the 'this' binding
person.getName();(In order of precedence)
- new (
new Person()) - explicit (
call,apply,bind) - implicit (
obj1.foo()) - default (
window, orundefinedin strict mode)
- arrow functions (
const hello = () => 'hi').thisis bound to thethiscontext that the function is defined in - ignored
this(passnullasthisto explicit binding - it will use the default binding)