Javascript 分配给数组的变量在数组更新时不更新(JS)
我将变量proxies赋值为数组。我试图在以后的某个时间点将项目添加到该数组中,然后在其他文件中访问该数组 问题是,proxyHelper.proxies值未更新以反映proxies变量的值 修改数组后,console.log(proxies)返回修改后的数组,但console.log(proxyHelper.proxies)返回空白。我需要访问其他文件中的proxyHelper.proxies值,以便您了解这是一个问题的原因 我在其他地方使用了类似的代码,而且效果很好——我没有看到什么Javascript 分配给数组的变量在数组更新时不更新(JS),javascript,node.js,Javascript,Node.js,我将变量proxies赋值为数组。我试图在以后的某个时间点将项目添加到该数组中,然后在其他文件中访问该数组 问题是,proxyHelper.proxies值未更新以反映proxies变量的值 修改数组后,console.log(proxies)返回修改后的数组,但console.log(proxyHelper.proxies)返回空白。我需要访问其他文件中的proxyHelper.proxies值,以便您了解这是一个问题的原因 我在其他地方使用了类似的代码,而且效果很好——我没有看到什么 var
var proxies = [];
proxyHelper.proxies = proxies;
proxyHelper.tester = function(win) {
electron.ipcMain.on('saveProxies', function(event, data) {
// Clear the previous proxies from list
proxies = [];
// Split proxies based on line breaks
if (data != '') {
let proxiesList = data.split('\n');
// i<= because we want to run a check to see if all proxies are added
for (let i = 0; i <= proxiesList.length; i++) {
// if the for loop hasn't ran through all proxies
if (i + 1 <= proxiesList.length) {
proxies.push(proxiesList[i]);
}
// Once it's loop through all proxies
else {
//Returns nothing
console.log(proxyHelper.proxies);
//Returns array with added items
console.log(proxies);
win.webContents.send('goodProxies', 'Saved!');
}
}
} else {
win.webContents.send('emptyProxies', 'Empty.');
}
})
}
var代理=[];
proxyHelper.proxies=代理;
proxyHelper.tester=函数(win){
electron.ipcMain.on('saveProxies',函数(事件,数据){
//从列表中清除以前的代理
代理=[];
//基于换行符拆分代理
如果(数据!=''){
让proxiesList=data.split('\n');
//我
您刚刚为此变量分配了一个新数组
proxyHelper.proxies
仍指向上一个值,不受影响
您应该始终使用单个变量,或者对其进行变异,而不是重新赋值
您刚刚为此变量分配了一个新数组
proxyHelper.proxies
仍指向上一个值,不受影响
您应该始终使用单个变量,或者对其进行变异而不是重新分配。不需要代理变量,因为它的更改不会反映到proxyHelper.proxy
只需使用proxyHelper.proxies
本身
此外,我还稍微整理了一下您的代码
您可以使用很多数组方法,而不是for
循环
proxyHelper.proxies = []
proxyHelper.tester = function(win) {
electron.ipcMain.on('saveProxies', function(event, data) {
//Split proxies based on line breaks
if (data != '') {
let proxiesList = data.split('\n');
proxiesList.forEach(function(proxy) {
proxyHelper.proxies.push(proxy)
})
win.webContents.send('goodProxies', 'Saved!');
} else {
win.webContents.send('emptyProxies', 'Empty.');
}
})
}
不需要proxy
变量,因为它的更改不会反映到proxyHelper.proxy
只需使用proxyHelper.proxies
本身
此外,我还稍微整理了一下您的代码
您可以使用很多数组方法,而不是for
循环
proxyHelper.proxies = []
proxyHelper.tester = function(win) {
electron.ipcMain.on('saveProxies', function(event, data) {
//Split proxies based on line breaks
if (data != '') {
let proxiesList = data.split('\n');
proxiesList.forEach(function(proxy) {
proxyHelper.proxies.push(proxy)
})
win.webContents.send('goodProxies', 'Saved!');
} else {
win.webContents.send('emptyProxies', 'Empty.');
}
})
}
以下是您的代码中发生的情况:
var proxies = []; // This new variable ("proxies") contains a reference to an empty array.
proxyHelper.proxies = proxies; // You assign the same reference to a property
// (named "proxies") of the "proxyHelper"
// object. The "proxies" property points
// to the same empty array as the variable above.
proxyHelper.tester = function(win) {
electron.ipcMain.on('saveProxies', function(event, data) {
proxies = []; // You assign a new reference to the global variable
// "proxies", which points to a new empty array.
// At this point the reference assigned to
// "proxyHelper.proxies" is still the original
// empty array from line 1
// ...
}
// ...
}
因此,当您访问“proxyHelper.proxies”时,您总是在访问从未修改过的原始空数组
你应该做的是:
proxyHelper.proxies = []; // resets the object's property to a new empty array
以下是您的代码中发生的情况:
var proxies = []; // This new variable ("proxies") contains a reference to an empty array.
proxyHelper.proxies = proxies; // You assign the same reference to a property
// (named "proxies") of the "proxyHelper"
// object. The "proxies" property points
// to the same empty array as the variable above.
proxyHelper.tester = function(win) {
electron.ipcMain.on('saveProxies', function(event, data) {
proxies = []; // You assign a new reference to the global variable
// "proxies", which points to a new empty array.
// At this point the reference assigned to
// "proxyHelper.proxies" is still the original
// empty array from line 1
// ...
}
// ...
}
因此,当您访问“proxyHelper.proxies”时,您总是在访问从未修改过的原始空数组
你应该做的是:
proxyHelper.proxies = []; // resets the object's property to a new empty array