错误:未捕获的TypeError:无法读取属性' length'未定义的

时间:2016-06-24 20:44:19

标签: javascript

我试图从我的构造函数中访问一个属性,但我继续得到" Uncaught TypeError:无法读取属性' length'未定义的。"我想要访问的属性是数组this.possibleX。请指教。谢谢!

var Item = function() {
    this.x = this.positionX();
    this.y = this.positionY();
    this.sprite = this.spritePic()
    this.possibleX = [0,100,200,300,400];
    this.possibleY = [80,160,240,320];
    this.spriteOptions = ['images/Rock.png','images/Rock.png','images/Rock.png','images/Heart.png','images/Gem Blue.png', 'images/Gem Green.png', 'images/Gem Orange.png'];
}

Item.prototype.positionX = function() {
    var startX = this.possibleX[Math.round(Math.random() * this.possibleX.length)];
    return startX;
}

var item = new Item();

1 个答案:

答案 0 :(得分:1)

var Item = function() {
    this.x = this.positionX();

注意你是如何首先调用该函数然后定义this.possibleX = [0,100,200,300,400];的。您需要反转此订单。