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 trial

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.