Javascript 为什么我要用这个画布?
可能重复:Javascript 为什么我要用这个画布?,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,可能重复: 我遇到了以下代码的问题: 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]/(
我遇到了以下代码的问题:
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);
在网页中运行会导致控制台中出现错误:
Uncaught ReferenceError: canvas is not defined
我必须使用
这个.canvas
。这只会发生在canvas
属性上,而不会发生在任何其他属性上。有人知道原因并能提供解释吗?谢谢 其他三个属性,camera
、mapSize
和width
都定义为函数参数,因此在函数范围内可用<代码>画布不是函数参数,也不是在函数中定义的,因此它不在作用域中。您需要区分变量和属性。构造函数中只有三个[局部]变量,即参数camera
、mapSize
和width
canvas
,height
等是您实例的属性,您可以通过使用指定一些值给它们。但不指定任何其他属性
尝试height
是的,我找到了……很抱歉我的愚蠢