What does call (this) mean in x.f2.call (this)

function X(){
    return object = {
        name:"object",
        f1(x){
            x.f2.call(this)        
        },
        f2(){
            console.log(this)
        }
    }
}

var options = {
    name:"options",
    f1(){},
    f2(){
        console.log(this)    
    }
}

var x = X()
x.f1(options)
Mar.16,2021

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
}
Menu