JavaScript 数组推送不是函数

标签 javascript arrays canvas html5-canvas queue

我想不出问题出在哪里。

var history = [];
.
.
.

window.onload = function() {
.
.
.
.


            history.push({
                xCoor: mousex,
                yCoor: mousey
            });
.
.
.
.

}

完整代码位于:fullCode

这是 HTML 部分:

<html>

<head>
</head>

<body>
    <h3>Canvas Example</h3>
    <canvas id="cv" width="600px" height="400px" style="border: 1px solid black"></canvas>
    <script src="rendering.js"></script>
    <p id="distance">Distance: </p>
</body>

</html>

当我开始画画时出现了这个错误:

TypeError: history.push is not a function

请帮帮我。非常感谢。

最佳答案

我正要提出一个建议,但后来我意识到您已将 history 设为全局范围的变量,也许您应该将其命名为其他名称,因为 window.history 完全是其他名称。

var otherHistory = [];

window.onload = function(){
    window.otherHistory.push({
        xCoor: mousex,
        yCoor: mousey
    });
}

关于JavaScript 数组推送不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364735/

相关文章:

javascript - 在 POST 请求中传递数据

javascript - 如何在javascript中动态添加项目到数组

javascript - 通过表单输入更新状态

javascript - 鼠标单击 Canvas 的一部分、鼠标悬停在 Canvas 上和鼠标移出 Canvas

javascript - 如何将默认图像添加到 Canvas ,同时让用户加载自己的图像

javascript - 如何在AG-Grid中设置列的动态字段?

javascript - JavaScript 中的奇怪数组行为

java - 修改数组中的标签

javascript - 重构复杂的多条件 if-else 语句

javascript - 如何使用 HTML5 在 Canvas 中绘制心电图监视器?