I have a javascript code and I added an event listener to my object. When I click it, it says my function is not defined. Here is my js code:
class Plane {
constructor(myMap, myPos, listener) {
this.createMarker(myMap, myPos);
this.clickListner = listener;
}
createMarker(myMap, myPos) {
var icon = {
url: "../Images/Plane.png", // url
scaledSize: new google.maps.Size(30, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(15, 15) // anchor
};
this.marker = new google.maps.Marker({
map: myMap,
icon: icon,
position: myPos
});
this.marker.addListener('click', function () { this.pressed(); }, false);
}
pressed() {
this.clickListner.onClick(this);
}
.
.
.
And I get this error when I press the marker:
Uncaught TypeError: this.pressed is not a function
Why doesn't it work?
Is not using the right context, you can solve this using an arrow function
this.marker.addListener('click',() => { this.pressed(); }, false);
^