나는 몇 년 동안 jQuery를 사용 해왔다. 바닐라 자바 스크립트에 대한 이해가 매우 제한되어있다. 범위, 객체 모델 및 자바 스크립트에서 사용되는 것으로 보이는 디자인 패턴 중 많은 것들이 나를 당황하게합니다. 내가 쓸 필요가있는 스케줄링 플러그인에서 결국 사용되는 클래스를 구현하려고하고 있는데 왜 내 클래스 멤버 중 하나에 저장된 데이터가 사용 가능한 것으로 보이지 않는지 이해하는 데 어려움을 겪고 있습니다. 문제가 범위 또는 내가 이해하지 못하는 다른 행동과 관련이 있는지 확실하지 않습니다.데이터 클래스 멤버가 직접 사용할 수없는 이유는 무엇입니까?
다음 코드는 적절한 위치의 주석에 2 개의 질문이 있습니다. 첫 번째 질문은 내 getJSON 호출에서 내 범위 해결 방법이 getJSON 내부에서 범위 문제를 처리하는 올바른 방법인지 여부입니다. 두 번째 질문은 내가 왜 schedule.data에 직접 액세스 할 수 없는지입니다.
function Schedule() {
this.year = null;
this.month = null;
this.day = null;
this.start_datetime = null;
this.start_timestamp = null;
this.end_datetime = null;
this.end_timestamp = null;
this.data = [];
return this;
}
Schedule.prototype.init = function() {
var url = '/tripsys/new_admin/employee_schedule/get_employee_schedule_data/' + this.start_timestamp + '/' + this.end_timestamp;
var self = this; // 1. trying to work around scope issues. Is this the correct way to handle the scope problems here?
$.getJSON(url, function(data) {
self.data = data;
});
}
var schedule = new Schedule();
$(document).ready(function() {
schedule.year = $('#year').text();
schedule.month = $('#month').text();
schedule.day = $('#day').text();
schedule.start_datetime = new Date(schedule.year, schedule.month - 1, schedule.day);
schedule.start_timestamp = Math.round(schedule.start_datetime.getTime()/1000);
schedule.end_datetime = new Date(schedule.year, schedule.month - 1, schedule.day, 23, 59, 59);
schedule.end_timestamp = Math.round(schedule.end_datetime.getTime()/1000);
schedule.init();
console.log(schedule); // if I log the whole schedule object the data that I expect to be in the "data" member is there
console.log(schedule.data); // 2. why is the data that I expect to be in the "data" member not there when I access schedule.data directly?
});
귀하의 통찰력에 감사드립니다.
보조 노트에, 당신은 정말 당신이 작업하고있는 언어를 이해한다 함께, 특히 2 년 동안 그것을 사용한 후에. 주제에 대해 조금 읽는 것이 좋습니다. –
동의합니다.과거에는 더 많은 백엔드 작업을 수행했으며 jQuery 추상화가 정말 멋지게 처리되는 간단한 DOM 조작보다 자바 스크립트로 더 많은 작업을 수행 할 필요가 없었습니다. 이제는 자바 스크립트에 대해 더 잘 이해할 시간입니다. 모두 도움을 주셔서 감사합니다. –
아, 그게 더 합리적입니다. 나는 지금 막 웹 프로그래밍을 가르치고있다. 물론 이것은 js를 배우는 것과 관련이있다. –