我正在尝试从外部 .js 文件调用函数,但控制台返回错误并且未调用该函数。如何更正我的代码并能够正确调用该函数。
这是主要的 .html 文件:
<html>
<head>
<script type="text/javascript" src="xp.js">
</script>
<script type="text/javascript">
window.onload = xyz.makeShape.Circle();
</script>
</head>
<body>
<canvas id="xyz" width="600" height="600"></canvas>
</body>
</html>
这是 .js 文件:
var xyz = xyz || {};
var canvas = document.getElementById('xyz');
var context = canvas.getContext('2d');
xyz.makeShape = {
Circle : function() {
console.log('working');
}
}
编辑
我在控制台中收到 2 个错误:
错误 1
TypeError: canvas is null
window.onload = xyz.makeShape.Circle;
错误 2
TypeError: xyz.makeShape is undefined
window.onload = xyz.makeShape.Circle;