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 trialAgata Zurek
Full Stack JavaScript Techdegree Graduate 26,419 Pointsactive = false as a constructor argument vs this.active = false;?
I'm curious why Ashley chose to write active = false in the constructor arguments rather than assigning this.active = false inside the constructor.
1 Answer
Steven Parker
231,248 PointsThat's the parameter named "active" that is being declared with a default value of "false". Parameters are always just names.
Inside the code block the attribute ("this.active") is assigned using the value of the parameter (just "active").
Agata Zurek
Full Stack JavaScript Techdegree Graduate 26,419 PointsAgata Zurek
Full Stack JavaScript Techdegree Graduate 26,419 PointsHi Steven, thank you for chiming in. Yes, it looks like I still haven't nailed down the various terms (argument, parameter, attribute), however, that wasn't what I was asking about so I will attempt to make it clearer.
What I'd like to know is why, in this instance, Ashley declared the "active" parameter with a default value instead of assigning that default value (false) directly inside the code block. As a counterpoint, In another portion of this challenge, she assigns attributes within the code block alone for the Board class with "this.rows = 6". I'm trying to figure out the logic of when to assign values inside a codeblock, when to provide optional default values and when to assign values when creating an instance of the class.
I haven't finished the challenge yet, so here's a guess as to where this is going: when we create instances of Player we will be able to assign one with the default value of "active = false" and the other player with "active = true" without having to add extra lines of code elsewhere to set the initial status?
Thanks!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsAssigning an initial value inside the code block always establishes that value, but defining a parameter with a default in the constructor allows you to specify a different value when creating an instance if you want, or leave it out and get the default. So the difference is whether you get the option or not.
Agata Zurek
Full Stack JavaScript Techdegree Graduate 26,419 PointsAgata Zurek
Full Stack JavaScript Techdegree Graduate 26,419 PointsHi again, yes, I get the part on what is going on, what I am still curious about is why it was implemented in this case. I've now finished the challenge and i saw it was used once, and not to any real benefit. I'm guessing the only reason the a parameter with a default was included was to demonstrate how it works: purely academic rather than providing much needed functionality. Is that about right? Thanks!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThe functional value of the default wouldn't be apparent from just one example. You would need to see it called in multiple ways, which would have required larger and more complex sample code.
As more versatile techniques are shown, you can probably expect that not every possible use case will be seen in the examples. The important thing is to know that the capabilities exist and are available to use when needed in real-world coding situations.
Happy coding!
Joe Elliot
5,330 PointsJoe Elliot
5,330 PointsI'm glad someone else asked this! Thanks for the answer Steven.