2017-05-09 4 views
2

HTML 5 캔버스 용 입자 시스템의 초기 단계에서 즉각적인 문제가 발생합니다. 내 Particle 클래스 객체의 속성을 검색하려고하면 undefined을 반환하고 이유를 파악할 수 없습니다!자바에서 객체 속성이 '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(); 
+0

맞춤법 오류를 말하는 빨간색 밑줄을 볼 때까지이 모든 것을 알아 내려고했습니다. 그것은 컨스트럭터가 아닌 생성자입니다 – Tremmillicious

답변

4

나는 당신이 당신의 Particle 클래스 생성자에서 오타를 줄 생각은 constructor을 읽어야합니다. 예를 들어 :

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

당신이 한 이후가되지 실제로P 변수를 초기화, 모든 속성은 디자인에 의해 undefined 있습니다.

+0

고맙습니다 .. * facepalm * – SkiZer0

관련 문제