JavaScript 数组推送不是函数

JavaScript Array push is not a function

我想不通问题出在哪里。

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
    });
}