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 trialja5on
10,338 PointsWhy (this) is acceptable answer but hasn't been taught yet.
(this.) never came across it???
Why hasn't (this) been taught to students before now? I had to find a cheat to this task on the internet? This isn't the first time either. We do pay for this service!
Another Question why use (this) as in w3schools says In this example, (this) represents the person object. In my thoughts person does also, so why ( person.isTurn ) doesn't work on this challenge.
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function(){
if ( this.isTurn ) {
}
}
}
2 Answers
Indiya Gooch
12,075 PointsIn JavaScript, the βthisβ keyword refers to the object it belongs to, the reason person.isTurn wouldn't work is for two reasons:
- this is a universal indicator to the current object in coding languages - which essentially means that to keep things simple and make it easier to transfer from one language to another this is used.
- You can have many objects of a class. So using this to refer to the current one is used to help build functionality easer.
Object oriented programming language (OOP) means that the language creates objects that hold both data and methods. Classes and objects are key aspects of any OOP.
A class is a template for objects, whilst an object is an instance of a class. When an object is created, it inherits all variables (properties) and methods of the class.
For example, think of a cookie cutter. The classes are the cookie cutter itself whilst objects are the cookies ( remember - objects are instances of a class).
Bec Asmar
4,403 PointsMan this is such a poorly written challenge hey? Like where were we supposed to come up with this.inTurn???
ja5on
10,338 PointsYes this.isTurn hasn't been covered yet either, I find that towards the end of most modules it very much goes awry.
ja5on
10,338 Pointsja5on
10,338 PointsSo I guess in a way OOP has its own set of laws that it must adhere to. Really good description btw. Thanks.