我在让 .push 在第一个之外的任何推送中向数组添加附加值时遇到问题。
var player_score = [];
function updatePlayerScore(x) {
player_score.push(x);
console.log(player_score);
}
第一次使用值 2
调用 updatePlayerScore(x)
时,数组已成功更新为 [2] 并显示在控制台中。
下次我使用任何值(即 4)调用 updatePlayerScore(x)
时,都会抛出错误“未捕获的类型错误: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);
关于javascript: array.push 第一次工作,后续调用错误为 "array.push is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39672564/