我收到此错误“Uncaught TypeError:无法在运行脚本时读取属性'未定义的属性'getContext'”。看起来变量“画布”是未定义的,但我不明白为什么。Uncaught TypeError:无法读取未定义的属性'getContext'
var world = {
canvas: document.getElementById("myCanvas"),
context: this.canvas.getContext("2d"),
centerX: this.canvas.width/2,
centerY: this.canvas.height/2,
drawShape: function (shape) {
if (typeof shape.draw() === "function")
shape.draw();
}
};
没关系,我想通了。我在世界文字外面声明了可变画布,现在它正在工作,但我仍不明白为什么你不能在里面声明。 –
'这'在JavaScript中非常奇怪。在你的情况下,你必须做 'context:world.canvas.getContext(“2d”)' 如果你不想在世界之外添加变量 –