javascript - 自定义对象的类型转换

时间:2016-10-26 20:20:19

标签: javascript types casting

我有2个自定义类:

//第一堂课

var MyPoint = function(x, y, size) {

    this.x = x;
    this.y = y;
    this.size = size;
};

MyPoint.prototype.paint = function(ctx) {
    //do some drawing in here
}

//第二课

var MyPath = function(a, b, c) {

    this.myPoints = new Array(); //array of the MyPoint objects
    this.size = 0;  //the size of the myPoints array
};

//add function add an object of MyPoint to the myPoints array
MyPath.prototype.add = function(mp) {
   index = 0;
   id (size > 0) {
       index = size -1;
    }
   this.myPoints[index] = mp;
   size++;
}

MyPath.prototype.paint = function(ctx) {

    for(I = 0; I < this.size; I++ {
        this.myPoints[I].paint(ctx);  //this is where my problem is
    }
}

我面临的问题是最后一行。当我运行此代码时,错误是paint方法未定义。当我尝试使用((MyPoint)this.myPoints [I] .paint(ctx)将第i个位置的数组元素转换为MyPoint时, 我得到了语法错误。如何正确输入演员?

0 个答案:

没有答案