构造函数中的函数以某种方式调用自身

Function in a constructor invokes itself somehow

本文关键字:方式 调用 函数 构造函数      更新时间:2023-09-26

此代码抛出错误 - "无法读取未定义的属性'x'"。我想分配这个函数并在以后用参数调用它(函数"crash"回答了关于与其他对象碰撞的问题)。

function Obj(x, y, height, width, type) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            if(type == "obstacle") {
                this.speedX = levelManager.level;
            }
            var self = this;
            this.isNotUsed = function() {
                return self.x < 0;
            };
            this.drawSelf = function(img) {
                ctx.drawImage(img, self.x, self.y, self.width, self.height);
            };
            if(type == "hero"){
                this.crash = function(otherObj) {
                    var myLeft = this.x;
                    var myRight = this.x + this.width;
                    var myBottom = this.y + this.height;
                    var otherLeft = (otherObj.x) || 0;  //error occurs here
                    var otherRight = (otherObj.x + otherObj.width) || 0;
                    var otherTop = otherObj.y || 0;
                    var collision = true;
                    if((myBottom < otherTop) ||
                        (myRight < otherLeft) ||
                        (myLeft > otherRight)) {collision = false;}
                    return collision;
                };
            }
        }
var hero = new Obj(0, 0, 40, 40, "hero");

代码运行良好(见片段)。如果您调用hero.crash()而不带任何参数,则可能出现的唯一错误。为了避免这种情况,您可以更改 crash() 函数添加为函数的第一行 otherObject = otherObject || {}; 。或者更好的是,正如注释中所建议的,如果 otherObject 未定义,只需返回:

if (!otherObject) return false;

或者如果它不是对象

if (typeof otherObject !== 'object') return false;

function Obj(x, y, height, width, type) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            if(type == "obstacle") {
                this.speedX = levelManager.level;
            }
            var self = this;
            this.isNotUsed = function() {
                return self.x < 0;
            };
            this.drawSelf = function(img) {
                ctx.drawImage(img, self.x, self.y, self.width, self.height);
            };
            if(type == "hero"){
                this.crash = function(otherObj) {
                    var myLeft = this.x;
                    var myRight = this.x + this.width;
                    var myBottom = this.y + this.height;
                    var otherLeft = (otherObj.x) || 0;  //error occurs here
                    var otherRight = (otherObj.x + otherObj.width) || 0;
                    var otherTop = otherObj.y || 0;
                    var collision = true;
                    if((myBottom < otherTop) ||
                        (myRight < otherLeft) ||
                        (myLeft > otherRight)) {collision = false;}
                    return collision;
                };
            }
        }
var hero = new Obj(0, 0, 40, 40, "hero");
console.log('THIS IS PRINTED')
console.log(hero.crash('{}'));
console.log('BUT FROM HERE NO MORE');
console.log(hero.crash());