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 trialJames Barrett
13,253 PointsWould using 'calculator' over 'this' work in this scenario? If not, why? And if it does, why would we choose 'this'?
Would calculator.sum += value; work in this scenario? If so, why is this.sum += value better?
Thanks, James.
3 Answers
Ben Myhre
28,726 Pointscontext. By using calculator.sum, you are referring to the variable from a more generic point of view. It is desirable to keep all logic within the current context, but by using 'calculator', you are changing the context to outside the inner workings of this object. Let's say I (Ben M.) was in a crowded room and I said "Ben is a baldy!!!" Well, the other 4 Bens in the room might be upset when really I was making a self deprecating joke about myself and talking about myself in the third person.
Using 'this' instead of 'calculator' helps clarify the context and keeps the logic inside your current function. 'this' is like me being in the same crowded room and saying "I am a baldy!!!" The other 4 Bens in the room cannot mistake my comment as being about them. I am limiting the context of what I am saying to be about ME, as an object.
Corina Meyer
9,990 Pointsbecause in the example an object literal is used, you could change the sum-property by calling calculator.sum += ... but this course aims to teach you how objects are build up and used. in object-context you work with instances of a defined type, meaning you define something like a template for an object and call methods on the specific object to change only that one and not the template in general. for example if you have a template for animals and an animal instance of spider you would be able to change the amount of legs for the spider through something like this.legCount = 8. but you would not want to change the template by setting animal.legCount = 8.
James Barrett
13,253 PointsI think I understand you. Is what your talking about to do with something called 'constructors'? Will this be covered further on in the course?
Jeremy Castanza
12,081 PointsWhile this is an involved topic, another take away that I got with using this vs. calculator is that it allows your code to be more dynamic and more maintainable. Let's say I decided to take your calculator code and I wanted to build a scientific calculator - my object might become more elaborate and change from "calculator" to "scientific_calculator". By changing my object definition, I'd need to do a find/replace on every "calculator" reference and replace with "scientific_calculator". If "this" is used throughout in your code, I'd only need to change my object definition without worrying about whether or not my code would break.
James Barrett
13,253 PointsJames Barrett
13,253 PointsHaha great answer! :D