0

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];
    }
3
  • 2
    You should simplify your code, make it the smallest possible example that stills exhibits the problem you are facing. That will help the people here locate the issue - and might help you fix it yourself! Commented Jun 5, 2013 at 16:11
  • 1
    The only property on your band objects are getColor, getNum, and getStrn, which are all methods. When you do var color = new Array(col, "");, it makes a "private" variable, that you can't access outside that constructor function...meaning you can't access opis_r1.color Commented Jun 5, 2013 at 16:12
  • One should avoid using new operator until he/she perfectly understands how this works and changes according to where you call it. It is a perfect trap and way to go for awkward bugs. Use simple objects, simple objects are cool. Commented Jun 5, 2013 at 16:15

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.