little pootis little pootis - 1 year ago 103
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)

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

Here is my HTML Code :

<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>ErikJs Unit Testing</title>
<script src="erik.core.js"></script>
<canvas id="myCanvas" width="500" height="400"></canvas>

var ctx = erik.initCanvas("#myCanvas");

ctx.moveTo(100, 200);
ctx.lineTo(200, 300);

And the JavaScript (erik.core.js) :

var Erik = function () { = "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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download