Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Object Basics Filling Out the Play Method

Why (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.

object.js
const player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){
        if ( this.isTurn ) {

        }
    }
}

2 Answers

In JavaScript, the ‘this’ keyword refers to the object it belongs to, the reason person.isTurn wouldn't work is for two reasons:

  1. 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.
  2. 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).

So I guess in a way OOP has its own set of laws that it must adhere to. Really good description btw. Thanks.

Man this is such a poorly written challenge hey? Like where were we supposed to come up with this.inTurn???

Yes this.isTurn hasn't been covered yet either, I find that towards the end of most modules it very much goes awry.