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"
.
-
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
2 Answers
Reset to default 5this
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
版权声明:本文标题:javascript - What's is "this" refer to in Phaser 3? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744669114a2618716.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论