所以我正在尝试创建一个颜色字符串数组,其下拉列表的选项值等于数组的索引,但是当我警告整个数组时,或只是一个值,它返回undefined。
function band(col, num, snum) {
var color = new Array(col,"");
var nmbr = num;
var strn = snum;
this.getColor = function() {
return color;
};
this.getNum = function() {
return nmbr;
};
this.getStrn = function() {
return strn;
};
}
var opis_r1 = new band("black", "", "");
var opis_r2 = new band("black", "", "");
var opis_r3 = new band("gold", "", "");
var opis_r4 = new band("gold", "", "");
var opis_r5 = new band("brown", "", "");
var resistance = "";
var resistance2 = "";
opis_r1.color[1] = "brown";
opis_r1.color[2] = "red";
opis_r1.color[3] = "orange";
opis_r1.color[4] = "yellow";
opis_r1.color[5] = "green";
opis_r1.color[6] = "blue";
opis_r1.color[7] = "violet";
opis_r1.color[8] = "grey";
opis_r1.color[9] = "white";
/*for (var i = 1; i < opis_r1.color.length; i++) {
opis_r2.color[i] = opis_r1.color[i];
}*/
opis_r2.color=opis_r1.color;
opis_r3.color[1] = "black";
opis_r3.color[2] = "brown";
opis_r3.color[3] = "red";
opis_r3.color[4] = "orange";
opis_r3.color[5] = "yellow";
opis_r3.color[6] = "greeen";
opis_r3.color[7] = "blue";
opis_r3.color[8] = "violet";
opis_r4.color[1] = "silver";
opis_r4.color[2] = "no color";
opis_r5.color[1] = "red";
opis_r5.color[2] = "orange";
opis_r5.color[3] = "yellow";
opis_r5.color[4] = "no color";
function hop2() {
alert("hop");
alert(opis_r1.color[0]);
gen_res_count();
}
function gen_check() {
var res;
var temp, temp1;
temp = opis_r1.color[document.forms['std_res_gen'].R11.value] + " - "+opis_r2.color[document.forms['std_res_gen'].R22.value] + " - " + opis_r3.color[document.forms['std_res_gen'].R33.value] + " - ";
res = temp;
return res;
}
function gen_res_count() {
if ((document.forms['std_res_gen'].R11.value == '?') || (document.forms['std_res_gen'].R22.value == '?') || (document.forms['std_res_gen'].R33.value == '?') || document.forms['std_res_gen'].R44.value == '?' || document.forms['std_res_gen'].R55.value == '?') {
window.alert("Choose values at all fields.");
document.forms['std_res_gen'].T33.value = "";
}
else {
resistance2 = gen_check();
alert("rcount");
gen_explain();
}
}
function gen_explain() {
document.forms['std_res_gen'].T33.value = resistance2 + opis_r4.color[document.forms['std_res_gen'].R44.value] + " - " + opis_r5.color[document.forms['std_res_gen'].R55.value];
}
答案 0 :(得分:4)
您访问的范围错误,请使用this.color
function band(col, num, snum) {
var self = this;
this.color = new Array(col,"");
var nmbr = num;
var strn = snum;
this.getColor = function() {
return this.color;
};
this.getNum = function() {
return nmbr;
};
this.getStrn = function() {
return strn;
};
}
或使用setter函数,因为你已经有了一个getter
function band(col, num, snum) {
var self = this;
var color = new Array(col,"");
var nmbr = num;
var strn = snum;
this.setColor = function(index,c) {
color[index] = c;
}
this.getColor = function() {
return color;
};
this.getNum = function() {
return nmbr;
};
this.getStrn = function() {
return strn;
};
}
然后使用你的getColor函数来获取数组
答案 1 :(得分:-1)
如果你这样做:
var color = new Array(col,"");
在函数内部,该函数范围的颜色仅为 。您应该执行以下操作:
var color = new Array();
function band(col, num, snum) {
color.push(col);
}