작은 프로젝트에서 Jquery를 제거하고 바닐라 js로 스크립트를 다시 작성하는 중입니다. 현재 코드에는 DOM 요소를 검색 한 다음 jquery 'find'를 사용하여 요소 내의 특정 요소를 검색하는 jquery 구현이 있습니다.자바에서 DOM 검색 빈도 줄이기
var ImageCapture ={
cacheDom : function(){
this.form = $('#drawingBoard');
this.saveBtn = this.form.find('#saveBtn');
this.image = this.form.find('#image');
this.results = this.form.find('#results');
}
}
위의 Jquery 코드를 다음과 같이 바닐라 js로 변환했습니다.
var ImageCapture ={
cacheDom: function() {
this.form = document.getElementById('drawingBoard');
this.saveBtn = this.form.querySelector('#saveBtn');
this.image = this.form.querySelector('#image');
this.results = this.form.querySelector('#results');
}
}
새로운 구현
잘 작동하는 것 같다하지만 난 바닐라 JS를 사용하여 JQuery와 구현을 대체하는 올바른 방법인지 확인하고 싶어?미리 감사드립니다.
네, 괜찮습니다. 정말 그걸로 잘못되거나, 개선 될 수는 없습니다. –
나를 위해 괜찮아 보입니다. 일관성을 위해'getElementById'를'querySelector'로 대체 할 수도 있습니다 –
ID 만 선택자로 사용하기 때문에 getElementById는 querySelector보다 약간 더 성능이 좋습니다. 그 점이 중요하다면 전환해야 할 수도 있습니다 (실제로는 거의 없지만 많은 작업을 수행하지 않는 한 어떤 차이도 느끼지 않을 것입니다). 그것 이외에, 그것은 모두 좋아 보인다. – delinear