Steve143 Steve143 - 2 years ago 140
Javascript Question

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


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


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.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download