Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

call function in canvas

I got error core.js:6241 ERROR TypeError: this.test is not a function at klass.<anonymous> when try to call function in canvas

HTML

<canvas #canvas id="canvas" width="900" height="400"></canvas>

ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ngOnInit() {

this.cvs.on("stagemouse:down", function(){
      console.log('Event mouse:down Triggered');
      this.test();
    })
  }

test(){
  console.log("test");
}

>Solution :

When defining a function with function(), it is not "bound", meaning the value of this might not always be the same depending on when and where the function is called. To declare a "bound" function, use the arrow syntax:

this.cvs.on("stagemouse:down", () => {
  console.log('Event mouse:down Triggered');
  this.test();
});

This syntax makes this in the function always refer to the this that is currently there when it is created.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading