所以最近我一直在寻找this code,并且我努力去理解一些东西。有人可以解释这些javascript的概念吗?
当“一流”是第一本下面的代码声明,我明白了:
function GravityPoint(x, y, radius, targets) {
Vector.call(this, x, y);
this.radius = radius;
this.currentRadius = radius * 0.5;
this._targets = {
particles: targets.particles || [],
gravities: targets.gravities || []
};
this._speed = new Vector();
}
我说得对不对说Vector.call(this, x, y);
使用,所以你不必申报的位置矢量?如果我理解正确,您可以拨打:
var a = new GravityPoint(0,0,10,[..]);
然后a.x == 0
? (假设向量具有x和y属性)
我的第二个问题是关于这段代码:
GravityPoint.prototype = (function(o) {
var s = new Vector(0, 0), p;
for (p in o) s[p] = o[p];
return s;
})({
gravity: 0.05,
isMouseOver: false,
dragging: false,
destroyed: false,
_easeRadius: 0,
_dragDistance: null,
_collapsing: false,
hitTest: function(p) {
return this.distanceTo(p) < this.radius;
}
...
});
我不明白这样的结构:MyClass.prototype = (function(o){})({ prop1:val1})
对于我来说,这就像发送第二部分({ prop1:val1}
)作为函数function(o){}
的参数,因为该函数返回一个向量,所以这是没有意义的。
感谢您的帮助
熟悉的[原型](https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)为了更好地理解这个评论典型的JS。基本上'GravityPoint'“从'Vector'继承”。这是通过将'GravityPoint'的原型设置为'Vector'类型的对象来完成的,所以如果前者访问方法/字段,那么如果未被重新定义,则最终在后者中搜索。这就是'return s'所做的。 for只是将参数的属性复制到用作原型的“Vector”中。这给他们一个默认值。 –
感谢您的明确解释! –