AddEventListener function doesn't recognize component function

Hello,

I’m working on scripts which move the camera around characters.
The behaviour is to : zoom on a character when I click on it (it works) and make the “return button” in html show itself.
When I click on the return button, the camera return on its original position.

Problem :
For it, I use on the start function of my component an addEventListener, like this :

this.returnBtn = document.getElementById("return-btn")

this.returnBtn.addEventListener("click", this.retourCamera)

The HTML button is correctly found, the function too, but problem :
All of my properties are “undefined” when I call the this.retourCamera with the addEventListener.

I review my code to be sure : correct name, correct use of this etc
But in red, the properties when I call the zoomCamera function
And in green when I call the retourCamera function (it’s the same console.log) :

console.log(this.ourCamera, " ", this.ourLookAt, " ", this.disableZooming)

Does somebody has an idea ?

Original Post on Discord

by user 224464722878005248

You need to bind your method when adding it (in addEventListener)

this.retourCamera.bind(this)

Thanks !

by user 224464722878005248