JavaScript에서 클래스 멤버를 이벤트 처리기로 사용하는 가장 좋은 방법이나 일반적인 방법이 있습니까?JavaScript의 이벤트 처리기로 클래스 메서드?
는 다음과 같은 간단한 예를 고려해
<head>
<script language="javascript" type="text/javascript">
ClickCounter = function(buttonId) {
this._clickCount = 0;
document.getElementById(buttonId).onclick = this.buttonClicked;
}
ClickCounter.prototype = {
buttonClicked: function() {
this._clickCount++;
alert('the button was clicked ' + this._clickCount + ' times');
}
}
</script>
</head>
<body>
<input type="button" id="btn1" value="Click me" />
<script language="javascript" type="text/javascript">
var btn1counter = new ClickCounter('btn1');
</script>
</body>
은 buttonClicked가 호출되는 이벤트 핸들러를하지만, _clickCount 멤버에 액세스 할 수 없습니다, 또는 다른 개체에 대한 이 점.
이런 종류의 문제에 대한 유용한 팁이나 기사/자료가 있습니까?
해결책 주셔서 감사합니다. 나는 또한 더 큰 그림 패턴, 습관 등을 이해하려고 노력하고있다. – JacobE
이 것은 훌륭합니다 : http://ejohn.org/apps/learn/ – pawel
이것은 좋은 예입니다. 그러나이 답변은 왜 여기가 초보자들에게 좋은 해결책인지에 대한 설명과 함께 더 낫습니다. –