Steve143
Steve143

Reputation: 55

TypeError: this.canvas is undefined (in declared object)

HTML

<canvas id="ctx"></canvas>

JavaScript

var objMap = {
htmlID: 'ctx',
canvas: document.getElementById(this.htmlID),
ctx: this.canvas.getContext('2d')};

In console the error shows up. "TypeError: this.canvas is undefined" I really want to have this in variables in object.

Upvotes: 1

Views: 670

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

No you can't do that with direct object. However with an annonymous function you can try building your object.

var objMap = new function () {
this.htmlID= 'ctx';
this.canvas= document.getElementById(this.htmlID);
this.ctx= this.canvas.getContext('2d');
};

Upvotes: 1

Related Questions