0

所以我试图创建一个颜色字符串数组,下拉列表的选项值等于数组的索引,但是当我警告整个数组或只是一个返回未定义的值时。

    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];
    }
4

2 回答 2

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 函数获取数组

于 2013-06-05T16:11:10.480 回答
-1

如果你这样做:

var color = new Array(col,"");

在函数内部,颜色对该函数范围有效。您应该执行以下操作:

var color = new Array();

function band(col, num, snum) {
    color.push(col);
}
于 2013-06-05T16:12:57.180 回答