繁体   English   中英

javascript:array.push首次运行,随后出现“ array.push不是函数”的调用错误

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

我在让.push在第一个推送以外的任何推送上向数组添加附加值时遇到问题。

var player_score = [];

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

我第一次使用值2调用updatePlayerScore(x)时,该数组已成功用[2]更新,并显示在控制台中。

下次我使用任何值(即4)调用updatePlayerScore(x) ,都会引发错误“未捕获的TypeError:player_score.push不是函数”

除非您在两个调用之间更改了player_score引用,否则它绝对可以正常工作。 请检查您的代码中player_score变量的用法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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