Bir değişkeni nasıl işlev olarak kullanabiliriz. Takip Javascript'te Üye İşlev
$(document).mouseup(function (evt) {
balls.push(new ball(mousePos["downX"],
mousePos["downY"],
5 + (Math.random() * 10), 0.9, randomColor()));
});
function ball(positionX, positionY, radius, color) {
this.px = positionX;
this.py = positionY;
this.rad = radius;
this.clr = color;
this.draw = drawFun(this.px, this.py, this.rad, this.clr);
}
function drawFun(x, y, r, c) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = c;
ctx.fill();
//stroke
ctx.lineWidth = r * 0.1;
ctx.strokeStyle = "#000000";
ctx.stroke();
}
for (var i = 0; i < balls.length; i++) {
//TODO: DRAW ALL BALLS
balls[i].draw;
}
Şimdi ben ball[i].draw;
kullanmak istiyorum benim kod ama konsolda bu beraberlik tanımsız olduğunu söyler. Nasıl Kullanım ball[i].draw(); // notice the parenthesis, to execute function.
ball[i]
Fare hareketi gerçekleşmeden önce, bu dizi dizisine nasıl erişeceksiniz? –
Evet, mouseUp'tan sonra bahsediyorum. Toplar dizisi, mouseUp içinde toplanıyor ve drawFun bir kez yürütüyor ama ben de idam için çalıştırılmasını istiyorum –
'this.draw = drawFun (this.px, this.py, this.rad, this.clr) ; 'bir işlev oluşturmuyor. This.draw = function() {....} 'gibi bir şeye ihtiyacınız var ya da drawFun bir işlev döndürüyor ya da işlevi olabilir: this.draw = drawFun; – mplungjan