简体   繁体   中英

Why is my array undefined

So I'm trying to create an array of color strings that a drop down has its options' values equal to the index of the array, however when I alert either the entire array or just a value it returns 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];
    }

you are accessing in the wrong scope, use 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;
        };

    }

or use a setter function since you already have a 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;
        };

    }

then use your getColor function to get the array

If you do:

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

inside the function, color will be valid only for that function scope. You should do the following:

var color = new Array();

function band(col, num, snum) {
    color.push(col);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM