그래서 내가하려는 것은 커스텀 객체의 배열입니다. 그래서 생성자가 있지만 매개 변수를 통해 값을 전달하지 않고 모든 값을 0으로 초기화하고 getter 또는 setter를 사용하여 값을 검색하려고합니다. 분명히 작동하지 않지만 왜 그런지는 알 수 없습니다. 어디에서나 매개 변수를 통해 값을 전달하여 객체를 만들었으므로 방금 추측했습니다.자바 스크립트에서 기본값을 사용하여 사용자 정의 객체의 배열 만들기
내 생성자
function Stol()
{
var cipsy = 0, kofca = 0, dzus = 0, sypcaj = 0, vrecaj = 0, coko = 0,jedlo = 0, pernik = 0, kava = 0, ucet = 0;
this.cipsy = cipsy;
this.kofca = kofca;
this.dzus = dzus;
this.sypcaj = sypcaj;
this.vrecaj = vrecaj;
this.coko = coko;
this.jedlo = jedlo;
this.pernik = pernik;
this.kava = kava;
this.ucet = ucet;
this.reset = reset;
function reset() {
this.cipsy = 0;
this.kofca = 0;
this.dzus = 0;
this.sypcaj = 0;
this.vrecaj = 0;
this.coko = 0;
this.jedlo = 0;
this.pernik = 0;
this.kava = 0;
Obrat += this.ucet;
this.ucet = 0;
}
this.addItem = addItem;
function addItem(type, number) {
switch (type) {
case 0 : this.cipsy += number; break;
case 1 : this.kofca += number; break;
case 2 : this.dzus += number; break;
case 3 : this.sypcaj += number; break;
case 4 : this.vrecaj += number; break;
case 5 : this.coko += number; break;
case 6 : this.jedlo += number; break;
case 7 : this.pernik += number; break;
case 8 : this.kava += number; break;
}
}
this.getItem = getItem;
function getItem(type) {
var item;
switch (type) {
case 0 : item = this.cipsy; break;
case 1 : item = this.kofca; break;
case 2 : item = this.dzus; break;
case 3 : item = this.sypcaj; break;
case 4 : item = this.vrecaj; break;
case 5 : item = this.coko; break;
case 6 : item = this.jedlo; break;
case 7 : item = this.pernik; break;
case 8 : item = this.kava; break;
}
return item;
}
}
는 여기에 내가 배열을
var stol = new Array();
for (var i = 0; i < 14; i++) {
stol[i] = new Stol();
}
을 만들고 결국 나는 다음과 같은 jQuery를 사용하여 일부 스팬을 수정할. #selecStol은 드롭 다운 목록입니다 (스위치가 완료되지 않았습니다).
$("#selecStol").change(function(){
var myStol = stol[$(this).find(":selected").val()];
for (i = 0; i < 14; i++) {
switch (i) {
case 0 : $("#cipsy").text(myStol.getItem(i));break;
}
}
})
하지만 작동하지 않으며 어느 부분인지 알 수 없습니다.
정확히 작동하지 않는 항목은 무엇입니까? 속성에 기본값이 할당되지 않습니다? –
그 코드를 디버깅 할 방법을 모르기 때문에 저도 모릅니다. – Slaaavo