HELP: Converting this arrow function...

Magusalfador

Veteran
Veteran
Joined
Jun 15, 2014
Messages
209
Reaction score
36
First Language
English Spanish
Primarily Uses
Hi, Im tryng to convert arrow functions to regular ones and viceversa just to understand them 100%.

But I cant make my code work this one:

Code:
setTimeout(() => this.colorOff(color), 350)
I converted to:

Code:
setTimeout(function () {
  this.colorOff(color);
}, 350);
The arrow function works but not the regular one...
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,340
Reaction score
3,043
First Language
Portuguese
Primarily Uses
RMMV
Arrow functions are nice because they remove the complexity of the this object. When you create a regular function instead, the value of this is different than when you create an arrow function.
The fix for you would be:

Code:
var that = this;
setTimeout(function () {
  that.colorOff(color);
}, 350);
 

Magusalfador

Veteran
Veteran
Joined
Jun 15, 2014
Messages
209
Reaction score
36
First Language
English Spanish
Primarily Uses
I see....
So, whenever Im converting arrow to regular functions I need to consider that if the keyword "this"is being used, I need to declare a variable = this first because "this" is not binded to the object?
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,340
Reaction score
3,043
First Language
Portuguese
Primarily Uses
RMMV
Yes, basically when you declare a new function, the "this" object will refer to that function. This used to cause a lot of unnecessary issues so when they added arrow functions, they made it keep the parent value of "this"
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Well, since I'm in lockdown, I guess it's finally time to get serious into rpgmaking... almost 20 years later.
Forbidden - You don't have permission to access /index.php on this server.
I love how people have real technical issues and im all like ''I CANT DECIDE ON A FONT'' :p

Forum statistics

Threads
95,745
Messages
931,516
Members
126,125
Latest member
ronylaplata
Top