Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将一个变量的值在同一个列表中多次按下?_Javascript_List_Variables - Fatal编程技术网

Javascript 如何将一个变量的值在同一个列表中多次按下?

Javascript 如何将一个变量的值在同一个列表中多次按下?,javascript,list,variables,Javascript,List,Variables,这是我的问题:我有一个函数,其中有一个变量,每次我调用这个函数时,它的值都会不断变化。在函数的后面代码中,我将变量推送到一个列表中。但是,当我多次这样做时,变量堆叠得很好,但所有值都与新值相同 var new_plane; //I define these variables here because I want to use it again in other functions var list_plane = []; var Planes = { //I define the obje

这是我的问题:我有一个函数,其中有一个变量,每次我调用这个函数时,它的值都会不断变化。在函数的后面代码中,我将变量推送到一个列表中。但是,当我多次这样做时,变量堆叠得很好,但所有值都与新值相同

var new_plane; //I define these variables here because I want to use it again in other functions
var list_plane = [];

var Planes = { //I define the object
    number: "",
    airline: ""
};

function add_plane() {
    new_plane = Planes;
    new_plane.number = 10; //Random number
    new_plane.airline = "Air France"; //Random airline of a list

    list_plane.push(new_plane); //I push the variable in the list

    for (let i = 0; i < list_plane.length; i++) {
        document.body.innerHTML += list_plane[i].number + " " + list_plane[i].airline + "<br />"; //The body is used for the example and the output too
    };
};
var新_平面//我在这里定义这些变量是因为我想在其他函数中再次使用它
变量列表_平面=[];
var Planes={//I定义对象
编号:“,
航空公司:“
};
函数add_plane(){
新平面=平面;
new_plane.number=10;//随机数
new_plane.airline=“法航”//列表中的随机航空公司
list_plane.push(new_plane);//我在列表中推送变量
for(设i=0;i”;//正文用于示例和输出
};
};

当第一架飞机进入时,一切正常。但是当设置第二个平面时,两个平面具有相同的值,而不是预期的各自值。

这是因为
new\u平面
引用同一对象,您将其多次推入数组

您可能希望每次初始化一个新对象,如下所示:

function add_plane() {
  const new_plane = {
    number: 10,
    airline: "Air France"
  };

  list_plane.push(new_plane);

  //...
}

但是,我建议将数组作为参数传递给add_plane函数,或者更好地使用OOP。

无论对该对象有多少引用(即使在同一数组中),总的来说,您仍然只有一个对象。您正在更新该对象的属性

而是定义要插入阵列的新对象:

function add_plane() {
    let new_plane = {
        number: 10, //Random number
        airline: "Air France" //Random airline of a list
    };

    list_plane.push(new_plane); //I push the variable in the list

    for (let i = 0; i < list_plane.length; i++) {
        document.body.innerHTML += list_plane[i].number + " " + list_plane[i].airline + "<br />"; //The body is used for the example and the output too
    };
};
函数添加平面(){
让新平面={
数字:10,//随机数
航空公司:“法航”//a列表中的随机航空公司
};
list_plane.push(new_plane);//我在列表中推送变量
for(设i=0;i”;//正文用于示例和输出
};
};

这是因为您需要创建一个新的平面实例:new_plane=new Planes()

您当前拥有的是一个声明的对象,平面,并且您不断更新该实例的值。无论是否将其添加到列表平面,该对象的任何其他用途都将更新

编辑: 您还需要将您的声明更改为:

function Planes () { //I define the object
    number = "";
    airline = "";
}
这就是如何使代码面向对象。我错过了早些时候的差异,很抱歉。

移动

var Planes = { //I define the object
    number: "",
    airline: ""
};
在函数内部,因为否则数组中的每个元素都有相同的对象引用


另一种解决方案是使用
平面
图案作为新对象的模板

// global
var Planes = {
        number: "",
        airline: ""
    };

function add_plane() {
    var new_plane = Object.assign({}, Planes); // get copy

    new_plane.number = 10;
    new_plane.airline = "Air France";

    list_plane.push(new_plane);

    for (let i = 0; i < list_plane.length; i++) {
        document.body.innerHTML += list_plane[i].number + " " + list_plane[i].airline + "<br />";
    } // no semicolon here
} // no semicolon here
//全局
变量平面={
编号:“,
航空公司:“
};
函数add_plane(){
var new_plane=Object.assign({},Planes);//获取副本
新平面编号=10;
new_plane.airline=“法航”;
列出平面。推送(新平面);
for(设i=0;i”;
}//这里没有分号
}//这里没有分号
var列表\u平面=[];
var Planes=函数(n,a){//I定义对象
这个数字=n;
this.airline=a;
};
功能添加_平面(数量,空气){
list_plane.push(new Planes(num,air));//我在列表中推送变量
}
功能打印飞机(){
for(设i=0;i”;//正文用于示例和输出
};
};
添加_飞机(10,“法航”);
添加_平面(15,“KLM”);
打印飞机();
从发展的角度来看,我认为这更清洁。有一点很重要:我认为应该从添加的方法中去掉for循环


这也可以为ES2015和其他版本重新编写,但我想这可能是下一次的家庭作业。

不,我不能,因为它告诉我飞机不能用作此类对象更改飞机声明为OOP将更为常见。@computercarguy,对,如果采用此概念,但其他概念也可以。