【问题标题】:Function in a constructor invokes itself somehow构造函数中的函数以某种方式调用自身
【发布时间】:2016-07-09 12:58:10
【问题描述】:

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

【问题讨论】:

  • 如何调用crash()函数?
  • 你的意思是crash函数在调用自己吗?
  • @gurvinder372 这也是我不明白的......
  • 你没有定义otherObj
  • @daniel432 没办法,您发布的代码有效(请参阅我的回答中的 sn-p)。您在其他地方还有其他代码,请发布完整示例。

标签: javascript class oop object


【解决方案1】:

代码运行良好(参见 sn-p)。如果您在没有任何参数的情况下调用hero.crash(),您可能会遇到的唯一错误。为避免这种情况,您可以将 crash() 函数更改为函数otherObject = otherObject || {}; 的第一行。或者更好,如 cmets 中所建议,如果 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());

【讨论】:

  • otherObj 初始化为空对象是没有意义的。如果你想支持这种情况,你应该只是 return false 如果没有争论
  • @Bergi 对,更新答案,谢谢。我写了return -1 因为return false 是一个有效的返回值。
  • 我猜 OP wants 如果意图真的是支持对没有参数的方法的调用,那么它的返回值是有效的。如果没有,你应该扔。从返回布尔值的函数返回 -1 听起来是个坏主意。
猜你喜欢
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 2012-01-30
  • 2022-10-19
  • 2020-03-12
相关资源
最近更新 更多