SkiZer0
SkiZer0

Reputation: 21

Object properties return 'undefined' on Javascript

I am encountering immediate issues in the early stages of this particle system for HTML 5 canvas. When I try to retrieve properties of my Particle class object, it returns undefined and I can't figure out why!

class Particle {
  contructor(context, width, height) {
    this.x = width / 2;
    this.y = height / 2;
    this.radius = Math.random() * 5 + 5;
  }
};

var App = {
  canvas: document.getElementById('canvas'),
  ctx: canvas.getContext('2d'),
  initialize: function() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  },
  draw: function() {
    var P = new Particle(this.ctx, this.canvas.width, this.canvas.height);

    alert(P.x); // Why does this return undefined?

    this.ctx.beginPath();
    this.ctx.arc(P.x,P.y,P.radius,0,2*Math.PI);
    this.ctx.stroke()
  }
};

App.initialize();
App.draw();

Upvotes: 2

Views: 215

Answers (1)

Paulo Mattos
Paulo Mattos

Reputation: 19339

I think you just have a silly typo in your Particle class constructor: it should read constructor. For instance:

class Particle {
    constructor(context, width, height) {
        ...
    }
};

Since you didn't actually initialized your P variable, all properties are undefined by design.

Upvotes: 4

Related Questions