运行一个对象方法有点麻烦,而且对于OOP来说很新颖。我有下面的构造类:原型方法返回“undefined”
function Blob(c, maxRadius, points, v) {
this.vector = v;
this.maxVector = 5;
this.center = c;
this.path = new Path();
this.path.closed = true;
for (var i = 0; i < points; i++) {
var delta = new Point({
length: (maxRadius * 0.5) + (Math.random() * maxRadius * 0.5),
angle: (360/points) * i
});
this.path.add(center + delta);
}
this.path.smooth();
this.path.fillColor = colors[i % 3];
};
然后我想原型以下方法给每个“斑点”对象
Blob.prototype = {
iterate: function() {
this.checkBorders();
if (this.vector.length > this.maxVector)
this.vector.length = this.maxVector
this.center += this.vector;
},
checkBorders: function() {
var size = view.size;
if (this.center.x < -this.radius)
this.center.x = size.width + this.radius;
if (this.center.x > size.width + this.radius)
this.center.x = -this.radius;
if (this.center.y < -this.radius)
this.center.y = size.height + this.radius;
if (this.center.y > size.height + this.radius)
this.center.y = -this.radius;
}
};
我再调用一个新的功能,反过来通过内部的构造类需要构造函数类的参数:
function createPaths() {
var radiusDelta = values.maxRadius - values.minRadius;
var pointsDelta = values.maxPoints - values.minPoints;
for (var i = 0; i < values.paths; i++) {
var radius = values.minRadius + Math.random() * radiusDelta;
var points = values.minPoints + Math.floor(Math.random() * pointsDelta);
var center = view.size * Point.random();
var vector = new Point({
angle: 360 * Math.random(),
length: Math.random() * 10
});
blobs.push(Blob(center, radius, points, vector));
};
}
但是,当我尝试从一个函数中获得它:
function onFrame() {
for (var i = 0; i < blobs.length - 1; i++) {
blobs[i].iterate();
}
}
返回
“遗漏的类型错误:无法读取的未定义的属性‘迭代’”,在我看来那么原型失败的原因?当我在onFrame()中调用“blob [i]”时,它会返回我期望找到的对象数组,因此它看起来不像它是类构造函数的任何问题......任何帮助都非常感谢!
这不是原型。你的'blobs'数组中有一个'undefined'值。 – Brennan 2014-11-20 18:51:03
我会添加到原型而不是重新定义它:'Blob.prototype.iterate = function(){...}' – sebnukem 2014-11-20 18:51:18
如何填充'blobs'数组? – 2014-11-20 18:53:23