아마도 일부 개념을 이해하지 못했지만 stackoverflow에서 4 개의 항목을 읽은 후 "typescript의 개인 멤버"에 대한 설명서를 읽은 후에도 여전히 혼란 스럽습니다.Typescript 개인 메서드는 "함수가 아닙니다"
간단한 마우스 IO (첫 번째 타이프 스크립트 프로젝트)를 작성했습니다. 이 고장 마우스 클래스의 전체 코드입니다 : 내가 onMouseDown
및 onMouseUp
내부 this.crossBrowserButton(e)
를 호출하는 데 사용
class Mouse {
public left: MouseKeyData = new MouseKeyData();
public right: MouseKeyData = new MouseKeyData();
public whell: MouseKeyData = new MouseKeyData();
public x: number = 0;
public y: number = 0;
private crossBrowserButton(e: any):string {
switch (e.button) {
case 0: return 'left';
case 1: return 'whell';
case 2: return 'right';
case 3: return 'back';
case 4: return 'forward';
}
return 'none';
}
private onMouseDown(e: any):void {
let target: MouseKeyData = this.left;
try {console.log(this.crossBrowserButton(e))} catch (a) {console.warn(a)} finally {}
if(target) {
target.press();
}
}
private onMouseUp(e: any):void {
let target: MouseKeyData = this.left;
if(target) {
target.release();
}
}
private onMouseMove(e: any): void {
this.x = e.pageX;
this.y = e.pageY;
}
public constructor() {
let anchor = document.body;
anchor.addEventListener('mousedown', this.onMouseDown);
anchor.addEventListener('mouseup', this.onMouseUp);
anchor.addEventListener('mousemove', this.onMouseMove);
}
}
,하지만 난 단지 this.crossBrowserButton is not a function(…)
을 얻었다.
this
범위를 잃어버린 것 같지만, this.left
은 완벽하게 작동합니다.
미리 감사드립니다.
이 오류는 저 숙련 된 세계에서 흔히 발생하며 잘못된 것이 무엇인지 파악했습니다. –