What's is “this” refer to in Phaser 3?

Clash Royale CLAN TAG#URR8PPP
What's is “this” refer to in Phaser 3?
I'm new to Javascript and want to make simple games with Phaser 3, and I found that Javascript seems to be a little different from other OOP languages like C++ or Java. I checked out the tutorial in the official website and some other tutorial page, most of the code is like:
var config =
...
scene:
preload: preload,
create: create,
update: update
var game = new Phaser.Game(config)
function preload()
this.load.img(...)
My question is what is the "this" in the preload() indicate to? Is it means the "game" we defined before?
"this"
preload()
"game"
And how to check the object's class in console? typeof() only tells "object".
typeof()
"object"
this
Possible duplicate of How does the "this" keyword work?
– mpm
Aug 12 at 11:07
2 Answers
2
The value of this depends on the scope of the function. In this case, and assuming this code is running in the browser, then this is equal to the window object.
this
function
this
window
this should be equal to the value that owns this code.
this
Reading
In the code you have this is a pointer to your game instance which is why you can call Phaser methods to load assets, adjust the camera, etc.
this
In your config you are setting which function is called during the preload step in the game. When Phaser runs it calls your function (which just happens to be named preload too) and sets the scope of this to the game instance.
preload
this
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Take a look at w3schools.com/js/js_function_invocation.asp. It explained
thisconcept simple– bahman parsamanesh
Aug 12 at 9:32