-1

I have a js script that works fine on its own, however when I try to move my code to reactjs the nested/referenced function loses 'this' and it references window instead. Pseudo code bellow. Any idea what causes this? I figure its related to webpack or something reactjs is doing?

lib.AnMovieClip = function() {
 this.testVariable = true;
 this.gotoAndPlay = function(){
  // 'this' works here perfectly fine
  this.otherFunction();
 }
 this.otherFunction(){
  // returns undefined, as 'this' is now the window object
  console.log(this.testVariable); 
 }
}
Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
Gibbz
  • 69
  • 4
  • 1
    could you rewrite that code in a manner where it actually runs - because ... as it stands ... the code wouldn't run, you couldn't run and how does `this.gotoAndPlay` get executed ... etc . – Bravo Sep 10 '21 at 06:21
  • It might be problematic that you are adding a `gotoAndPlay()` method on MovieClip, which already has one. What is `this` in each function? – Lanny Sep 11 '21 at 16:08

1 Answers1

0

I ended up finding that the value was not being assigned correctly. this.testVariable was being overwritten.

Gibbz
  • 69
  • 4