对象属性返回';未定义';关于Javascript

对象属性返回';未定义';关于Javascript,javascript,html5-canvas,undefined,javascript-objects,Javascript,Html5 Canvas,Undefined,Javascript Objects,在HTML5画布的粒子系统的早期阶段,我遇到了直接的问题。当我尝试检索我的Particle类对象的属性时,它返回undefined,我不知道为什么 class Particle { contructor(context, width, height) { this.x = width / 2; this.y = height / 2; this.radius = Math.random() * 5 + 5; } }; var App = { canvas:

在HTML5画布的粒子系统的早期阶段,我遇到了直接的问题。当我尝试检索我的
Particle
类对象的属性时,它返回
undefined
,我不知道为什么

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();

我认为你的
粒子
类构造函数中有一个愚蠢的输入错误:它应该是
构造函数
。例如:

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

由于您没有实际初始化
p
变量,因此所有属性都是未定义的

我一直想弄明白,直到我看到一条红色的下划线写着拼写错误。它是构造函数而不是构造函数