javascript - 为什么我必须使用 this.canvas?

标签 javascript html html5-canvas

Possible Duplicate:
Javascript: Do I need to put this.var for every variable in an object?

我在使用以下代码时遇到了问题:

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

我必须使用 this.canvas反而。这只发生在 canvas 上属性(property),但不是任何其他属性(property)。有谁知道原因并可以提供解释吗?谢谢!

最佳答案

其他三个属性,cameramapSizewidth 都定义为函数参数,因此在函数作用域内可用。 canvas 不是函数参数,也没有在函数内部定义,因此它不存在于作用域内。

关于javascript - 为什么我必须使用 this.canvas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14251031/

相关文章:

javascript - Axios 泛型 Post 在 TypeScript 中返回错误类型

javascript - 无法使用javascript获取xml文件中标签的内容

javascript - 此上下文在分配时发生变化

html - 如何使 flexbox 响应?

reactjs - React-konva 上传后如何更改图像?

javascript - 动态添加的 div 溢出其容器

jquery - 使用 JQuery 启用/禁用 ASP.NET Web 表单是否需要禁用输入的验证器?

css - html 不加载本地 css 文件

javascript - 严格模式下ImageData数据赋值

javascript - 在canvas中上传多张图片转换为一张图片(Html5)