The function of
call
is to change the this
value of a function call and provide parameters for the function call.
var x = X ()
, when x
is the object
object,
x.f1 (options)
, because F1
is called as an attribute of the global variable x
, when executing x.f1
, the this
in the method points to the global variable x
, passing in the parameter options
.
f1(x){
x.f2.call(this) // thisxxxxoptions
}
replace the variables in the above code:
options.f2.call(x) // xoptionsthisx
When
options.f2
is executed, because f2
is called as an attribute of options
, by default this
points to the options
object, but call (x)
is used, that is, when the method is called, explicitly set this
to point to the global variable x
:
f2(){
console.log(this) // thisxx
}