我想不出问题出在哪里。
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/