0

我有这个代码:

const settings = {
            notes: [new musicnote(480,400,"wav/1.wav","cyan",true)],
            emiters: [new emiter(300,240), new emiter(340,240)]
        }

var notes = [];
var emiters = [];

var LoadLevel = function (level) {
    if(settings[level] !== undefined){
        currentLevel = level;
        notes = settings[level].notes;
        emiters = settings[level].emiters;
    }
}

当我emiters在游戏中更改数组(删除或添加一些)时,它对settings.emiter数组的作用相同。所以我的问题是 - 如何在settings对象中传递一个新数组而不是对数组的引用?

4

1 回答 1

4

你可以这样做Array.slice

var original = [];
var copy = original.slice();

// to test:
copy.push("foo");
alert(original.length); // 0
于 2013-04-27T22:21:49.057 回答