Possible Duplicate:
Javascript: Do I need to put this.var for every variable in an object?
I ran into a problem with the following code:
var BuildMiniMap = function(camera, mapSize, width){
this.camera = camera;
this.canvas = document.getElementById('gameCanvas');
this.mapSize = mapSize;
this.width = width;
this.height = Math.floor(mapSize[1]/(mapSize[0]/width));
alert(canvas);
}
var miniMap = new BuildMiniMap(camera, [800, 600], 200);
Running it in a webpage will lead to an error in console:
Uncaught ReferenceError: canvas is not defined
And I have to use this.canvas instead. This only happens with canvas property, but not any other property. Does anyone know the reason and can provide an explanation? Thanks!
The other three properties,
camera,mapSizeandwidthare all defined as function arguments so are available within the function scope.canvasis not a function argument and is not defined inside the function so it doesn’t exist within the scope.