Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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_Arrays - Fatal编程技术网

Javascript数组在非预期情况下得到更新

Javascript数组在非预期情况下得到更新,javascript,arrays,Javascript,Arrays,我正在为一个小型服务器开发一个discord机器人,我遇到了一个小问题。我这里有一个模板变量: let blankProfile = { ID: '', name: '', maxStrength: 10, currentStrength: 10, maxHealth: 100, currentHealth: 100, maxMana: 0, currentMana: 0, maxDefense: 0, curre

我正在为一个小型服务器开发一个discord机器人,我遇到了一个小问题。我这里有一个模板变量:


let blankProfile = {
    ID: '',
    name: '',
    maxStrength: 10,
    currentStrength: 10,
    maxHealth: 100,
    currentHealth: 100,
    maxMana: 0,
    currentMana: 0,
    maxDefense: 0,
    currentDefense: 0,
    maxSpeed: 0,
    currentSpeed: 0,
    magic: 0,
    spells: [

    ],
    money: 0,
    position: [1,1],
    items: [

    ],
    isFighting: false,
    wins: [

    ], 
    losses: [

    ]
}
我试图改变其中的一个值。我有一个全局变量,用于读取和解析JSON文档,我将模板变量添加到此函数中:

 function createFighter(userID, fighterName) {

    for(var i = 0; i < playerstats.length; i++) {
        console.log(playerstats[i].ID);
        if(playerstats[i].name == fighterName) {
            return "Please choose a different name."; //To prevent duplicates
        }
        if(playerstats[i].ID == userID) return "You already have a fighter!";
    }   
    blankProfile.name =  fighterName; //this does not "stick" outside of the function
    blankProfile.ID = userID;  //neither does this
    playerstats.push(Object.assign(blankProfile));
    fs.writeFileSync('playerstats.json', JSON.stringify(playerstats, null, 2));
    blankProfile.name = "";
    blankProfile.ID = "";
    console.log(playerstats); //returns desired output, with the correct name and ID


    return "Success!";
}
函数createFighter(userID,fighterName){
对于(变量i=0;i

当我测试这个函数时,结果表明playerstats正确地更新了name和id,但是当我在调用函数后尝试
console.log(playerstats)
时,它返回一个数组,其中name和id为空。为什么会发生这种情况?

问题是您正在访问和修改对象的属性
blankProfile
。这违反了数据不变性的原则,如果不小心,可能会导致代码库中出现错误。我鼓励您创建一个函数(生成器、构造函数),它返回空的概要文件(每次调用新对象时都返回新对象)。例如:

const getBlankProfile = () => ({
    ID: '',
    name: '',
    maxStrength: 10,
    currentStrength: 10,
    maxHealth: 100,
    currentHealth: 100,
    maxMana: 0,
    currentMana: 0,
    maxDefense: 0,
    currentDefense: 0,
    maxSpeed: 0,
    currentSpeed: 0,
    magic: 0,
    spells: [

    ],
    money: 0,
    position: [1,1],
    items: [

    ],
    isFighting: false,
    wins: [

    ], 
    losses: [

    ]
})
然后,只要在您想要创建新对象的任何地方使用此函数即可

function createFighter(userID, fighterName) {

    for(var i = 0; i < playerstats.length; i++) {
        console.log(playerstats[i].ID);
        if(playerstats[i].name == fighterName) {
            return "Please choose a different name."; //To prevent duplicates
        }
        if(playerstats[i].ID == userID) return "You already have a fighter!";
    }   
    const blankProfile = getBlankProfile();
    blankProfile.name =  fighterName; //this does not "stick" outside of the function
    blankProfile.ID = userID;  //neither does this
    playerstats.push(blankProfile);
    fs.writeFileSync('playerstats.json', JSON.stringify(playerstats, null, 2));
    console.log(playerstats); //returns desired output, with the correct name and ID


    return "Success!";
}
函数createFighter(userID,fighterName){
对于(变量i=0;i
您定义了
blankProfile
。当你说:

blankProfile.name = "";
blankProfile.ID = "";
JS将转到
blankProfile
,并将这些值更改为
'
,如此处指定的那样

要不显示该信息,请不要参考
blankProfile

let fakeBlankProfile = blankProfile;
fakeBlankProfile.name = "";
fakeBlankProfile.ID = "";
然后,如果要更新它:

blankProfile = fakeBlankProfile;

不是因为您正在将
name
ID
设置为
,就在
fs.writeFileSync(…
)之后。另外,您在何处以及如何创建变量
playerstats
对象。分配(blankProfile)
需要一个目标。您可能需要
对象。分配({},blankProfile)
。在这个答案中,blankprofile和fakeblankprofile指向同一个对象。更改其中一个将更改另一个。不,请尝试。
fakeblankprofile
具有相同的
blankprofile
值,但不引用它。(它只是JS!)如果您说
fakeBlankProfile.property=value
,JS将更新
fakeBlankProfile
的值,但将不使用
blankProfile