javascript - 未捕获的类型错误 : Cannot read property 'canvas' of undefined - Javascript Object

标签 javascript jquery canvas javascript-objects

我对如何使用、定义、更改和取消设置 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 is not a function on line 29 (index.html / Ship.init())

我现在该怎么办?

最佳答案

问题是 this.canvas 没有指向对象 shipcanvas 属性,它指向全局对象 窗口。您需要以不同方式初始化 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);
        }
    }
}();

关于javascript - 未捕获的类型错误 : Cannot read property 'canvas' of undefined - Javascript Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461334/

相关文章:

javascript - 在 Angular 中隐藏数组中的不匹配元素

javascript - 使用 .map() 将对象属性转换为数组到 HTML

javascript - 使用 Canvas 和 ForEach 处理异步调用

javascript - 如何在 Canvas 上制作类似powerpoint可调文本的文本?

javascript - 使用 Web 套接字将 Canvas 内容从客户端传递给管理员

javascript - 如何从字符串值中仅获取部分文本?

JavaScript 和处理浮点确定性

javascript - 使用node.js + socket.io 发送通知

jquery - 如何使用 jQuery 为文本颜色设置动画?

jquery - Spring Mvc jsp页面在使用js和css文件初始化日历后给出空白输出