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 trialDavid Aguirre
8,170 PointsHow const space = new Space(x,y) is created?
I want to ask the relationship between the variable space and the space class that is created in the Space.js file.
There is no relationship between then?
By using new Spaces (x,y) - does this means that the browser will read this as the updated Space class ?
new Space(x,y) - is similar to creating a class and a constructor all in that single line?
I would like to advance this course but I don't think this was explained well.
2 Answers
Steven Parker
231,275 PointsThe relationship is that "Space" is the class, and "space" is the variable that is assigned an instance of that class.
The "new" keyword creates an instance of a class. It runs the constructor as part of making the new object.
It might help to think of a class as a kind of blueprint for how to make an object. It's not an object itself, but you can use it to make one.
Theresa Secore
7,001 PointsIf still unclear, think of Space class in Space.js as the 'original' object and variable space as the copy of that object, and variable space has to adhere by all the rules (ie constructor parameters) when replicating itself and inheriting all the characteristics (ie. all the properties of Space class in the constructor and methods outside of the constructor) of the 'original' object (Space class in Space.js). You can replicate (instantiate) a new Space class object in any file as you see fit to use by using the keyword 'new' (ie new Space())
David Aguirre
8,170 PointsDavid Aguirre
8,170 PointsI think I mistyped a couple of things.
For instance the Space class created in the Space.js file and the instance you are referring to new Spaces are related right?
If not then this is making a class from scratch by doing " new Space (x,y)" and by making this statement with parenthesis after that is like putting a constructor method?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsYou're not making a class here, the class was already made in the other file. You are making an instance object using that class as the "blueprint".
And yes, the constructor is invoked in the creation process.