从前一个函数添加到数组

时间:2015-09-12 22:26:44

标签: javascript

所以我想访问我在上一个函数中创建的数组。然后在id1,id2,id3中,我想创建一个新属性并为该属性赋值。我以为我知道我应该做什么,但我做的事情不起作用。我得到错误IdArray未定义。

HTML:

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

javascript以前的功能:

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新功能:

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

我输了,所以如何从上一个函数访问数组并添加到它?

4 个答案:

答案 0 :(得分:2)

您无法访问其他函数中的变量,但您可以访问在函数上方声明的变量。您真正的问题是无法理解构造函数,这将在未来对您有所帮助。看看这个:

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

答案 1 :(得分:1)

您需要保存IdArray或将其返回:

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
    }

然后使用它。 注意我删除了var关键字,所以我使用的是全局IdArray。

答案 2 :(得分:1)

变量IdArray是在函数内部创建的,因此它在函数结束时到期。如果添加行

var IdArray;

在您的第一个功能之前,它将使其全局化。然后在任一函数中引用IdArray将引用相同的变量。

答案 3 :(得分:1)

我将IdArray添加为.rotate_close对象的属性,以赋予其全局范围:

window