Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development Game Development with Phaser Building a Breakout Game with Phaser Phaser Project Setup

What happens to the object literal (config) when it gets passed in? How is the Phaser.Game class built to process it?

Prior to this course, I haven’t really seen an object literal being passed as an argument to the constructor method of a class. I knew it was possible but haven’t seen how things work under the hood exactly. More specifically, how the object literal gets broken down and utilized within the class definition. With primitives, it’s pretty clear. Parameter names represent passed in values. Those values get assigned to what are essentially “variables” using the this keyword by way of assigning those “variables” the defined parameter names (i.e. this.name = name). Then those “variables” (i.e. this.name) get utilized throughout the class definition. What I’m looking for is some clarity (at a high level) as to what happens when the passed in argument is an object literal like we have with the Phaser.Game constructor method.

At a high level, when we pass the configuration object literal to the Phaser.Game constructor method, are the values of the key-value pairs within the configuration object literal being assigned to variables and/or keys with the same name as the keys within the configuration object but that belong to other object literals defined within the Phaser.Game class? So, essentially, the Phaser.Game class is built in a way to expect values of specific key/property names (as those key/property names are littered throughout the class definition, including its method definitions) and then the class will utilize those values in the creation of its instances, method definitions, etc.? Is this more or less the correct way of thinking about things?

1 Answer

Steven Parker
Steven Parker
231,184 Points

Passing an object literal is essentially the same as passing a variable containing an object. In either case, from inside the function the parameter refers to the object. All keys in the object literal are now associated with the parameter name.

The main difference is that after the function returns, there is no longer any reference to the object and it will be disposed by background memory management.