我遇到了一个问题:我有一个函数,在这个函数中有一个变量,每次调用该函数时它的值都在改变。在函数的代码后面,我把这个变量推入一个列表中。但是当我多次执行这个操作时,变量都被正确地存储,但是所有的值都与最新的值相同。
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
};
};
当输入第一个平面时,一切都很好。但是设置第二个平面时,两个平面具有相同的值,而不是预期的自己的值。