little pootis little pootis - 20 days ago 8
Javascript Question

JavaScript: uncaught reference error: erik is not defined

I'm writing a small utility that would make my working with canvas easier. But, when I run the code, I get the errors

Uncaught SyntaxError: Unexpected token = (erik.core.js:5)

and
Uncaught ReferenceError: erik is not defined (test.html:14(
(anonymous function)
.

Here is my HTML Code :

<!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>


And the 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();

Answer

Further to johusman's answer, default parameters are now a part of ECMAScript 6, meaning this should be perfectly valid on JS engines supporting this feature. For engines not yet supporting default parameters, the esnext project provides a transpiler, available from the project GitHub page.

Comments