Can an object's property be assigned with a statement? Consider the following bit of code:
var obj = {
canvas: document.getElementById('mainCanvas').getContext('2d');
}
The way I understand that this doesn't work is because you're not actually 'assigning' the statement to the canvas
property of obj
. Instead I've done something like this:
var obj = {
initCanvas: function() {
this.canvas = document.getElementById('mainCanvas').getContext('2d');
}
}
This works, but it seems sloppy to me. Is there a better way to define this or is using this method the preferred way to implement what I'm talking about?