Mohammed Hammed Mohammed Hammed - 1 year ago 91
Javascript Question

Little Issue with javascript, help please! thanks

I'm trying to run this code, where the constructor is the ball function, and the

is the prototype which should include the code in ball.

Can't seem to display the drawing of the ball.

Help would be greatly appreciated.

var canvas = document.getElementById('ok');
var d = canvas.getContext('2d');

function Ball() {
this.x = 100;
this.y = 100; };

var circle = function(x, y, radius, fillCircle){
d.arc(x, y, radius, 0, Math.PI * 2, false);
} else{
d.stroke(); } };

Ball.prototype.draw = function() {
d.lineWidth = 2;
d.strokeStyle = "black";
d.fillStyle = "red";
circle(this.x, this.y, 30, true); };


Answer Source

When using the prototypes in JS, you still would need to instantiate a new instance of the object you're modeling.

So your code is fine, except the last line; instead you want to create a new ball and then call the member function draw on it, like so:

var ball = new Ball();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download