나는 초보자 프로그래머입니다. OO 방식으로 스크립트를 만들고 싶습니다. 그러나 내 속성을 보호하고 액세스 할 수있는 방법을 생각할 수 없습니다.자바 스크립트 - 이벤트 함수에서 객체 속성에 액세스하는 방법?
var imageViewer=function(imageWrapper)
{
var hasTouch = 'ontouchstart' in window,
resizeEvent = 'onorientationchange' in window ? 'orientationchange' : 'resize',
startEvent = hasTouch ? 'touchstart' : 'mousedown',
moveEvent = hasTouch ? 'touchmove' : 'mousemove',
endEvent = hasTouch ? 'touchend' : 'mouseup',
cancelEvent = hasTouch ? 'touchcancel' : 'mouseup';
this.imgContainer=imageWrapper;
this.image=imageWrapper.getElementsByTagName("img")[0];
this.initScale=1;
this.scaleLevel=this.initScale;
this.startPoint={x:0,y:0}
//alert(this.image)
this.initEvents=function()
{
window.addEventListener(resizeEvent, this.resizeImageViewer, false);
this.imgContainer.addEventListener(startEvent, this.handleStartEvent, false);
this.imgContainer.addEventListener(moveEvent, this.handleMoveEvent, false);
this.imgContainer.addEventListener(endEvent, this.handleEndEvent, false);
}
this.resizeImageViewer=function(event)
{
/*not finish*/
}
this.handleStartEvent=function(event)
{
/**********problem goes here*************/
this.startPoint.x = event.offsetX || (event.pageX - this.imgContainer.offsetLeft);
this.startPoint.y = event.offsetY || (event.pageY - this.imgContainer.offsetTop);
}
this.handleMoveEvent=function(event)
{
/*not finish*/
}
this.handleEndEvent=function()
{
/*not finish*/
}
this.initEvents();
}
var imageViewerObj = new imageViewer(document.getElementById("imageWrapper"));
내가 this
을 알고 :
"TypeError: this.imgContainer is undefined"
전체 스크립트는 다음과 같습니다 : 문제는 그 기능
this.handleStartEvent=function(event)
방화범의 오류 메시지에 내 this.XXX
속성에 액세스하지 못할 것입니다 imgContainer
이 아니고 imageViewer
이 될 것입니다. 그러나 제게는 imageViewer
님의 부동산 정보를 얻는 방법을 모르겠습니다. e startPoint
및 initScale
. 누군가 나를 안내 할 수 있을까요?
잘 작동합니다! 고맙습니다 ~ 해결책을 ~ –
@ 크리스. 오신 것을 환영합니다. – bniwredyc