Problems in the use of setInterval function in js

function Test(){
    this.func1=function(){
        console.log("func1")
    }
    this.func2=function(){
        console.log("func2")
    }
    this.func3=function(){
        console.log("func3")
    }
    this.all=function(){
        console.log(this)
        this.func1()
        this.func2()
        this.func3()
    }
}
var test=new Test()
test.all()//thisfunc1func2func3
setInterval(test.all,1000)//thiswindow

Why does this suddenly point to window? in the setInterval (test.all,1000) statement?

the code I am writing now requires the statement setInterval (test.all,1000) . How can I make the code work properly?

ask for answers from the great gods, especially confused

Mar.04,2021

your setInterval (test.all,1000) is equivalent to

setInterval(function(){
        console.log(this)
        this.func1()
        this.func2()
        this.func3()
    },1000)

this is out of the context of test , so this points to window

.

to execute normally, upload the test context: so
setInterval (test.all.apply (test), 1000) is fine


bind object goes in
setInterval (test.all.bind (test), 1000);

Menu