简体   繁体   中英

javascript: array.push works the first time, subsequent call errors with “array.push is not a function”

I'm having a problem getting .push to add an additional value to an array on any push beyond the first.

var player_score = [];

function updatePlayerScore(x) {
    player_score.push(x);
    console.log(player_score);
}

The first time I call updatePlayerScore(x) with a value of 2 , the array is successfully updated with [2] and displays in the console.

The next time I call updatePlayerScore(x) with any value (ie 4) an error is thrown "Uncaught TypeError: player_score.push is not a function"

It is absolutely working fine unless you have changed the player_score reference between two calls. Please check your code for usage of player_score variable.

 var player_score = []; function updatePlayerScore(x) { player_score.push(x); console.log(player_score); } updatePlayerScore(2); updatePlayerScore(4); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM