HELP: Converting this arrow function...

Magusalfador

Veteran
Veteran
Joined
Jun 15, 2014
Messages
240
Reaction score
41
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,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
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
240
Reaction score
41
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,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
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 Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,078
Members
137,580
Latest member
Snavi
Top