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

Why does the "this" keyword not refer to the Player instance after it is created? Why does it point to the window obj?

I understand that without the bind() method, the this keyword used in props.onScoreChange within the Application component will point to the Window object. I don't understand why it wouldn't just point to the Player instance that was created?

It seems to me that if the function is inside of the Player component and contains the this keyword that it should point to the object to which the function belongs, that being the Player instance in this case.

What's going on here?

1 Answer

This question comes from the React Basics course (https://teamtreehouse.com/library/react-basics).

Here's the code...

Counter component

function Counter(props) {
    return (
        <div className="counter">
          <button className="counter-action decrement" onClick={function() {props.onChange(-1);}}> - </button>
          <div className="counter-score"> {props.score} </div>
          <button className="counter-action increment" onClick={function() {props.onChange(1);}}> + </button>
        </div>
    );
}

Player component

function Player(props) {
  return (
    <div className="player">
      <div className="player-name">
        {props.name}
      </div>

      <div className="player-score">
        <Counter score={props.score} onChange={props.onScoreChange}/>
      </div>
    </div>
  );
}

Player.propTypes = {
  name: React.PropTypes.string.isRequired,
  score: React.PropTypes.number.isRequired,
  onScoreChange: React.PropTypes.func.isRequired
}

Application component

const Application = React.createClass({
    propTypes: {
        title: React.PropTypes.string,
        initialPlayers: React.PropTypes.arrayOf(React.PropTypes.shape({
            name: React.PropTypes.string.isRequired,
            score: React.PropTypes.number.isRequired,
            id: React.PropTypes.number.isRequired
        })).isRequired
    },

    getDefaultProps: function() {
        title: "My Scoreboard"
    },

    getInitialState: function() {
        return ({
            players: this.props.initialPlayers,
        });
    },

    onScoreChange: function(index, delta) {
        console.log('onScoreChange', index, delta);
    },

    /*
     * Confused as to why we use bind here... Why wouldn't "this" refer to the Player once the instance of the Player is created?
     * I think what's going on here is that the "this" is instead bound to the enumerated instance of the Player, meaning
     * that once the function is done with the particular instance it no longer exists and as a result points to the Window?
     */

    render: function() {
        return (
            <div className="scoreboard">
                <Header title={this.props.title}/>

                <div className="players">
                  {this.state.players.map(function(player, index) {
                      return (
                        <Player
                            onScoreChange={function(delta) {console.log(this)}}
                            name={player.name}
                            score={player.score}
                            key={player.id}/>
                        );
                    }.bind(this))}
                </div>
            </div>
        );
    }
});

this does not point to the Player component. It looks like it's pointing at the Counter.

Steven Parker
Steven Parker
243,318 Points

You might want to move this addendum into the question itself (or to a comment) and then delete this answer, to make it clear to people browsing the forum that this question hasn't been answered yet.