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

时间:2016-07-09 12:58:10

标签: javascript class oop object

此代码抛出错误 - “无法读取未定义的属性'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");

1 个答案:

答案 0 :(得分:1)

代码运行良好(请参阅代码段)。如果您在没有任何参数的情况下调用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());