Open buchuitoudegou opened 5 years ago
context
probably means the value of this
in the same scope. If a function defined in a scope, the value of this
will be global
when it's called directly. In other word, the context
of the function is global
unless call
, apply
or new
are used or called by other object (e.g. obj.func()
).
Furthermore, if call
, apply
or new
are used to call a function, the context
of the function will be changed while the context
of the functions defined in it will not.
function a() {
console.log(this.b);
const that = this;
function b() {
console.log(this === that);
}
b();
}
a.call({ b: 'b' })
a();
output:
b
false
undefined
true
When a function is called, 3 steps are executed:
this
output:
why
func1
andfunc2
share the same context?