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

Jennifer Riess
Jennifer Riess
6,551 Points

I'm confused as to where the 'this' statement is inserted. They didn't lead up to this in the video, confused.

sos

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

1 Answer

Andrew Hickman
Andrew Hickman
Full Stack JavaScript Techdegree Student 10,012 Points

In this case, this would refer to the particular instance of player1. So instead of:

if (player1.isTurn){
}

you would chain isTurn to this:

if (this.isTurn){
}