我正在编写一个小实用程序,可以让我更轻松地使用画布。但是,当我运行代码时,我得到了错误Uncaught SyntaxError: Unexpected token = (erik.core.js:5)
和Uncaught ReferenceError: erik is not defined (test.html:14(
(anonymous function)
.
这是我的 HTML 代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ErikJs Unit Testing</title>
<script src="erik.core.js"></script>
...
</head>
<body>
...
<canvas id="myCanvas" width="500" height="400"></canvas>
<script>
var ctx = erik.initCanvas("#myCanvas");
ctx.beginPath();
ctx.moveTo(100, 200);
ctx.lineTo(200, 300);
ctx.stroke();
</script>
</body>
</html>
和 JavaScript (erik.core.js):
var Erik = function () {
this.author = "Erik Royall";
};
Erik.prototype.initCanvas = function ( element, y = '2d' ) {
this.canvas = document.querySelectorAll( element );
this.context = canvas.getContext( y );
return this.context;
};
var erik = new Erik();