Javascript -克隆的全局变量被覆盖

Javascript - Cloned global variable getting overwritten

本文关键字:覆盖 全局变量 -克 Javascript      更新时间:2023-09-26

我在网页上有一组图表。我想保留一份他们的数据副本& &;DIV ID,然后再修改它们,以便我可以随时重用静态全局副本。(这将是图表的原始数据数组)。同样的数据还有一个全局可变的副本,叫做"globalMasterList"。

这是我到目前为止所尝试的。我遇到的问题是,尽管使用"克隆"方法来备份静态副本,但似乎有一些可更改的全局副本的参考。当我更改全局可变副本时,静态全局副本也会更改。我花了几天时间研究这个问题,但还是没有弄清楚。我也试过一些不同的东西,但都没有运气。

如果有人能帮我解决这个问题,我将不胜感激。

我在网页上有一组图表。我想保留一份他们的数据副本& &;DIV ID,然后再修改它们,以便我可以随时重用静态全局副本。(这将是图表的原始数据数组)。同样的数据还有一个全局可变的副本,叫做"globalMasterList"。

这是我到目前为止所尝试的。我遇到的问题是,尽管使用"克隆"方法来备份静态副本,但似乎有一些可更改的全局副本的参考。当我更改全局可变副本时,静态全局副本也会更改。我花了几天时间研究这个问题,但还是没有弄清楚。我也试过一些不同的东西,但都没有运气。

如果有人能帮我解决这个问题,我将不胜感激。

function modifyGroupingDefault() {
    var numDivs = 5;
    var divDrawId = 'div5';

    if (staticCopyoFGlobalCopy.length !== numDivs) {    
        staticCopyoFGlobalCopy.length = 0;  //  Clear the array just in case some junk elements have sneked in
        for (var x in masterList) { // Iterate through the charts on the web page
                var tempObj = {};   //  Temporary object
                tempObj.plotId = clone(globalMasterList[x].div.id); // Clone Div ID
                tempObj.divData = clone(globalMasterList[x].data);  // Clone the dataset
                staticCopyoFGlobalCopy.push(tempObj);   //  Push the cloned object into array
        }
    }
    for (var x in globalMasterList) {
        if (globalMasterList[x].div.id === divDrawId) { // Check if the current Div ID in the iteration loop is the Div ID of the chart whose groupingbutton is clicked
            delete globalMasterList[x].dataset; //  Remove the chart's data provider
            for (y in staticCopyoFGlobalCopy) { //  Iterate through the static/non-changable global copy
                if (staticCopyoFGlobalCopy[y].plotId === divDrawId) {   
                    globalMasterList[x].data = staticCopyoFGlobalCopy[y].divData;   // Set the current chart's data to the dataset from the immutable chart array
                    // Some operation on globalMasterList
                    //Some operation onglobalMasterList
                    // Some operation onglobalMasterList
break;  // Goal achieved. break out
                }
            }
            break;  // Goal achieved. Break out
        }
    }
}
/*
    *
    *   Clone function
    *
*/
function clone(obj) {
    var copy;
    // Handle the 3 simple types, and null or undefined
    if (null === obj || "object" !== typeof obj)
        return obj;
    // Handle Date
    if (obj instanceof Date) {
        copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }
    // Handle Array
    if (obj instanceof Array) {
        copy = [];
        for (var i = 0, len = obj.length; i < len; ++i) {
            copy[i] = clone(obj[i]);
        }
        return copy;
    }
    // Handle Object
    if (obj instanceof Object) {
        copy = {};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr))
                copy[attr] = clone(obj[attr]);
        }
        return copy;
    }
    throw new Error("Unable to copy obj! Its type isn't supported.");
}

早就解决了。