admin管理员组

文章数量:1391955

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?

And how to check the object's class in console? typeof() only tells "object".

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?

And how to check the object's class in console? typeof() only tells "object".

Share Improve this question edited Feb 19, 2020 at 15:49 user47589 asked Aug 12, 2018 at 9:11 kevinHuangkevinHuang 1652 silver badges7 bronze badges 2
  • Take a look at w3schools./js/js_function_invocation.asp. It explained this concept simple – Bahman Parsa Manesh Commented Aug 12, 2018 at 9:32
  • 2 Possible duplicate of How does the "this" keyword work? – mpm Commented Aug 12, 2018 at 11:07
Add a ment  | 

2 Answers 2

Reset to default 5

this is an instance of Phaser.Scene and not Phaser.Game.
The other answers are incorrect. The code is running the browser.

To see the docs for a Scene you can look here

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.

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.

本文标签: javascriptWhat39s is quotthisquot refer to in Phaser 3Stack Overflow