0

so I would like to access an an array i created in a previous function. and then in id1,id2,id3, i would Like to create a new property and give that property a value. I thought I have an idea of what I should do but what Im doing is not working. I get the error IdArray is undefined.

HTML:

        <p id="show_me"></p>
        <button onclick="ObjectArray()">click me</button>
        <p id="added"></p>
        <button onclick="Added()">Add</button>

javascript previous function:

var ObjectArray = function() {
        // object literal
        var id1 = {
            firstName: "John",
            lastName: "Doe",
            id: "12345"
        };

        // keyword new
        var id2 = new Object;
        id2.firstName = "Adam";
        id2.lastName = "Bakely";
        id2.id = "abcdef";

        // object constructor 
        function employee(first, last, id) {
            this.firstName = first;
            this.lastName = last;
            this.id = id;
        }
        var id3 = new employee("Dallas", "Star", "abc123");

        //create an array
        var IdArray = [id1, id2, id3];
}

javascript new function:

function Added(IdArray, sex){
    IdArray.push({sex : sex})
    IdArray[0].sex = "male";
    document.getElementById("added").innerHTML = IdArray[0]; 
}

im lost, so how do I access the array from the previous function and add to it?

2
  • Declare you IdArray in global. Commented Sep 12, 2015 at 22:29
  • don't think this will work like expected.. innerHTML is better with a string document.getElementById("added").innerHTML = IdArray[0] Commented Sep 12, 2015 at 22:33

4 Answers 4

2

You do not have access to variables in other functions, you can however access variable declared above your functions. Your real problem is a failure to understand Constructors, which will greatly benefit you in the future. Check this out:

function Employee(id, firstName, lastName, middleName){
  this.id = id; this.firstName = firstName; this.lastName = lastName;
  this.middleName = middleName;
  this.notes = [];
  this.getFullName = function(){
    var mn = this.middleName ? ' '+this.middleName : '';
    return this.firstName+mn+' '+this.lastName;
  }
  this.createNote = function(note){
    this.notes.push(note);
    return this;
  }
  this.getNotesString = function(delimiter){
    return this.notes.join(delimiter);
  }
}
var employee1 = new Employee('abc123', 'Joe', 'Schmoe');
employee1.createNote('Note You Want to Store.');
employee1.createNote('Just Another Test Note.');
var employee2 = new Employee('def456', employee1.firstName, 'Smith', 'Walter'); // notice how you access the other instance firstName
console.log(employee1.getFullName());
console.log(employee1.getNotesString('|'));
console.log(employee2.getFullName());
console.log(employee2.createNote('Note 1').createNote('Note 2').createNote('Note 3').getNotesString('|')); // by returning `this` you can access other methods in the same Constructor
Sign up to request clarification or add additional context in comments.

3 Comments

could you exlpain like Im five, what exactly is going on here, I got some idea of whats going on, but still lost.
the first function is using object constructor to create an object type "employee", which contains firstName, lastName, middleName.... then I think "this.notes=[]; creates a note object... this.getfullname creates an object function? to return full name.. unsure of how creating a var mn and then useing this.firstname+mn+ works, confused to what exactly happens here.... this.createnote function allows you to push text into the note array.... a little confused on this part and then next function.... think I may not understand the full use of "this" as well
Look into ternary expressions. this in this case refers to the individual instance of an Object, not another Object.
1

You need to either save the IdArray or to return it:

var IdArray;
    var ObjectArray = function() {
            // object literal
            var id1 = {
                firstName: "John",
                lastName: "Doe",
                id: "12345"
            };

            // keyword new
            var id2 = new Object;
            id2.firstName = "Adam";
            id2.lastName = "Bakely";
            id2.id = "abcdef";

            // object constructor 
            function employee(first, last, id) {
                this.firstName = first;
                this.lastName = last;
                this.id = id;
            }
            var id3 = new employee("Dallas", "Star", "abc123");

            //create an array
            IdArray = [id1, id2, id3]; //removed the var keyword, so I am using the global variable
    }

and then use it. notice I removed the var keyword so I am using the global IdArray.

Comments

1

The variable IdArray was created inside the function, so it expires when the function ends. If you add the line

var IdArray;

before your first function that will make it global instead. Then reference to IdArray inside either function will refer to the same variable.

Comments

1

I added the IdArray as a property of the window object to give it global scope:

var ObjectArray = function() {
        // object literal
        var id1 = {
            firstName: "John",
            lastName: "Doe",
            id: "12345"
        };

        // keyword new
        var id2 = new Object;
        id2.firstName = "Adam";
        id2.lastName = "Bakely";
        id2.id = "abcdef";

        // object constructor 
        function employee(first, last, id) {
            this.firstName = first;
            this.lastName = last;
            this.id = id;
        }
        var id3 = new employee("Dallas", "Star", "abc123");

        //create an array
        window.IdArray = [id1, id2, id3];
}

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.