Reputation: 55
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
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