Garbage Collector: this vs global Variables

Status
Not open for further replies.

Eliaquim

Raze: The Rakuen Zero's Guardian!
Veteran
Joined
May 22, 2018
Messages
962
Reaction score
388
First Language
Portuguese - Br
Primarily Uses
RMMV
Hi people!
I'm really trying to find out this information on the internet, but I can't find it.

Since the keyword "this" let me access a property of a function outside of it, I want to know if the garbage collector behaves the same way as it behaves with global variables.
So far as I know, javascript free the memory when an object or variable lost its reference.
So in case of this keyword being used constantly, it behaves like a local variable that can be accessed outside the function? Or its behaves like a global variable(but not being global or local variable)?
My concern is performance.

So it's best for me to use this to change the property of a sprite for example:
this.carSprite.opacity = 100;

Or it's best for me to make a function that returns the opacity?

Function carSpriteOpacity(){
return 100;
}

Here is an example of information that tells me to use the "this" keyword to avoid using global variables.
But what confuses me is it's saying that it is different from the global variable, but it also can be accessed like a global variable outside of the function. So what I'm missing here?

Screenshot_20200119-144318.png
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
2,347
Reaction score
3,652
First Language
English
Primarily Uses
RMMV
I think there needs to be some explaining to be done on "this" in javascript. It refers to the current instance. I know it sounds abstract, but here's a list of what "this" refers to depending on the situation:
  • In a method, this refers to the owner object.
  • Alone, this refers to the global object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer this to any object.
So since it refers to an existing active object, it can't refers something that was removed via garbage collection. Garbage collection is automatic in javascript and it only removes data that have 0 reference in the application. Here's an example:
Code:
var newActor = new Game_Actor(); // this adds data to the memory by creating a new actor
newActor = null; // this sets the previously created variable to null. Therefore the data about
                 // the Game_Actor object used for newActor isn't referenced anymore in the code and
                 // will be removed by the garbage collection the next time it is called.
 

Eliaquim

Raze: The Rakuen Zero's Guardian!
Veteran
Joined
May 22, 2018
Messages
962
Reaction score
388
First Language
Portuguese - Br
Primarily Uses
RMMV
@MushroomCake28 thanks for the explanation! Well, I have to admit that was a little hard for me to express this one, but you help me a lot!
So one last question:
Well, let's see I have that function:

JavaScript:
Sprite_Timer.prototype.createBitmap = function(){
this.bitmap = new Bitmap(w,h);
this.bitmap.textColor = "white";
// define other objects to the bitmap.
}

Sprite.prototype.doSomething = function(){
let color = this.bitmap.textColor;
};
In the function " doSomething" I'm accessing the property of the bitmap "textColor" using the keyword "this".
In that case, this is a global object(because I'm accessing him outside the scope of the function that I defined him)?

Because, if it was indeed a global object, I would like to prefer using a Global variable(I don't like to see this keyword in my code xD).

So I think now I can talk better about my doubt:

When " this" keyword is a global object, it means the same, in terms of performance, as a Global Variable?
Using one or another is just a matter of concept(on how you want to build your code)?
Or I'm totally wrong(if yes, please help me rsrsrs).
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
2,347
Reaction score
3,652
First Language
English
Primarily Uses
RMMV
In your example this refers to Sprite_Timer.prototype and the third one refers to Sprite.prototype. Both are objects and this will refer to the object itself. You have written that code the correct way.

The difference between objects and variables is not relevant to the keyword this. Variables are containers for values, and so are objects. In that sense objects are variables too in javascript. The difference is that an object can store many values. For more information, I highly suggest reading this (very easy to understand):
https://www.w3schools.com/js/js_objects.asp

When we talk about a global variable or a global object, global means that it is directly under the application's main process if you want. So in this instance, this would effectively be equivalent to window. It's just a container for everything running. Type window or this in the console while in-game and you'll see what I mean.

I don't understand why you don't want to use this in your code? It's one of the central concept of javascript and it's used all the time. Just check the rpg_windows.js or rpg_scenes.js files and you'll see what I mean.

Have you coded in another language? If so I might point toward the "equivalent" of this in other languages.
 

Eliaquim

Raze: The Rakuen Zero's Guardian!
Veteran
Joined
May 22, 2018
Messages
962
Reaction score
388
First Language
Portuguese - Br
Primarily Uses
RMMV
Ohhh!! So it was like I suspected!!
I'm getting now the "this" keyword thing. So it's ok to use it. I think I have not fully completely understand this keyword until this point, because of that I was being afraid to use it. But now I get it!
Thank you so much for taking the time to kill my doubts!! *____*
Let's return to plugin making now!!! :kaoluv:
 

hiddenone

Lurker Extraordinaire
Global Mod
Joined
Feb 19, 2014
Messages
2,144
Reaction score
4,841
First Language
english
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

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

Latest Threads

Latest Profile Posts

Well it's been almost a whole year since I joined RMW, so i figured I should update my status.
This is my updated status.
Here's a sample of some pixel edits I made.

Maybe for use as sidewalks.
If there are any mods or admins online, could you please see about the re-translation pages for RPG Maker MV? They are all down :rsad:
Werewolf cons: you lose control of yourself once a month and could accidentally kill your friends. Vampire cons: no garlic bread. The choice is simple. Werewolf for sure.
2 huge market drops in 2 days. So glad I sold my non-dividend stocks about 2 weeks ago. And if the dividend ones drop heavily, hey more cheap stocks for me!

Forum statistics

Threads
94,525
Messages
921,603
Members
124,377
Latest member
Darkyyy
Top