【问题标题】:Uncaught TypeError: Cannot read property 'canvas' of undefined - Javascript Object未捕获的类型错误:无法读取未定义的属性“画布” - Javascript 对象
【发布时间】:2016-02-01 09:00:11
【问题描述】:

我对如何使用、定义、更改和取消设置 javascript 对象感到困惑。

我的 javascript 对象是:

var Ship = function(){
    return {
        canvas: $('#area')[0],
        canvas_width: this.canvas.width,
        canvas_height: this.canvas.height,
        context: this.canvas.getContext("2d"),
        ship_image: new Image(),
        ship_width: null,
        ship_height: null,
        ship_x: 0,
        ship_y: 0,
        init: function() {
            this.ship_image.onload = function() {
                this.ship_width = this.width;
                this.ship_height = this.height;
                this.ship_x = (this.canvas_width / 2) - (this.ship_width / 2);
                this.ship_y = this.canvas_height - this.height;

                this.draw(
                    this.ship_x,
                    this.ship_y
                );
            }

            this.ship_image.src = "ship.gif";
        },
        draw: function(x, y) {
            this.context.drawImage(
                this.ship_image,
                x,
                y
            );
        }
    }
}();

当我像这样执行这段代码时;

$(function(){
    Ship.init();
    Controller.init();
});

我每次都会收到这个错误。

Uncaught TypeError: Cannot read property 'width' of undefined on line 4 (ship.js)

Uncaught TypeError: Ship 不是第 29 行的函数 (index.html / Ship.init())

我现在该怎么办?

【问题讨论】:

    标签: javascript jquery canvas javascript-objects


    【解决方案1】:

    问题是this.canvas 没有指向对象shipcanvas 属性,它指向全局对象window。您需要以不同方式初始化 canvas_width,例如在 init 函数中。 context 也一样:

    var Ship = function () {
        return {
            canvas: $('#area')[0],
            ship_image: new Image(),
            ship_width: null,
            ship_height: null,
            ship_x: 0,
            ship_y: 0,
            init: function () {
    
                this.context = this.canvas.getContext("2d");
                this.canvas_width = this.canvas.width;
                this.canvas_height = this.canvas.height;
    
                this.ship_image.onload = function () {
                    this.ship_width = this.ship_image.width;
                    this.ship_height = this.ship_image.height;
                    this.ship_x = (this.canvas_width / 2) - (this.ship_width / 2);
                    this.ship_y = this.canvas_height - this.ship_image.height;
    
                    this.draw(this.ship_x, this.ship_y);
                }.bind(this);
    
                this.ship_image.src = "ship.gif";
            },
            draw: function (x, y) {
                this.context.drawImage(this.ship_image, x, y);
            }
        }
    }();
    

    【讨论】:

    • context: this.canvas.getContext("2d"), 同样的 :-)
    • 如何在init中达到draw函数?因为draw是;未捕获的 TypeError:this.draw 不是函数。
    • @R.CanserYanbakan 是的,确实,上下文还有一个问题。检查更新的答案代码。问题是在this.ship_image.onload 处理程序this 内部指向图像实例对象。因此,您可以简单地将这个函数绑定到正确的this 并使用this.ship_image.width 而不是this.width
    • 使用默认值定义上下文、canvas_width 和 canvas_height 并在 init 函数中更改这些值是否更好?
    • 是的,你可以先定义它们,然后在init中初始化,这是一个偏好问题。 context: null, canvas_width: null.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2012-07-04
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多