所以最近我一直在尋找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」中。這給他們一個默認值。 –
感謝您的明確解釋! –