Prototype acces local variables

You must Login before you can answer or comment on any questions.

function Team() {
 
 
 
    // Player
    this.playerList;
 
 
 
    Team.prototype.getPlayerByID = function( requestedUserID ) {
 
        for( var i=0; i<playerList.length; i++)
        {
            if( this.playerList[i].user_id == requestedUserID )
            {
                return this.playerList[i];
            }
        }
    };
 
 
 
}
 
 
module.exports = Team;

I am trying somehting like the example above, but i can't acces my playerList variable iside of my prototype funciton... Am i doing something wrong or is it just not possible? (so i have to parse the list with the requestedUserID to the function)

??

1 Answer

Hi Nico

You have to call the prototype outside of the function you are extending.

function Team() {
...
    // Player
    this.playerList; 
...
}
Team.prototype.getPlayerByID = function (requestedUserID) { 
    for (var i=0; i<this.playerList.length; i++) {
        if (this.playerList[i].user_id === requestedUserID) {
            return this.playerList[i];
        }
    }
};
module.exports = Team;

Your Answer

Think you can help? Login to answer this question!