MooTools는 기능에 대해 protect
메서드를 제공하므로 Class
외부에서 호출되지 않도록 보호하려는 모든 메서드에서 MooTools를 호출 할 수 있습니다. 그래서 당신은 할 수 있습니다 :
var Notifier = new Class({
showMessage: function(message) {
},
setElementClass: function(klass) {
}.protect()
});
var notifier = new Notifier();
notifier.showMessage();
notifier.setElementClass();
> Uncaught Error: The method "setElementClass" cannot be called.
은하지 않는 것이
class
자바 스크립트의 미래에 예약 된 키워드입니다 그것을 사용할 때 코드가 깨질 수 있습니다. 이 시점에서 Safari는 확실히 파손되지만 다른 브라우저의 동작도 보장되지 않으므로
class
을 식별자로 사용하지 않는 것이 좋습니다.
protect
을 사용하는 장점 중 하나는이 클래스를 확장해도 여전히 하위 클래스의 보호 된 메서드에 액세스 할 수 있다는 것입니다. 당신은 접두사 또는 방법 이전 또는 이후에 _
접미로 명명 규칙을 사용하려면
Notifier.Email = new Class({
Extends: Notifier,
sendEmail: function(recipient, message) {
// can call the protected method from inside the extended class
this.setElementClass('someClass');
}
});
var emailNotifier = new Notifier.Email();
emailNotifier.sendEmail("a", "b");
emailNotofier.setElementClass("someClass");
> Uncaught Error: The method "setElementClass" cannot be called.
는, 그뿐만 아니라 완벽하게 괜찮아요. 또는 _
과 보호 된 메소드를 결합 할 수도 있습니다.
이것은 내가 찾던 바로 그 것이다. 감사합니다. 다음에 mootools 문서를 두 번 확인해야 할 것입니다. – aubreyrhodes