Warum ändert das Ändern einer Kopie eines Arguments das Argument?

Ich habe die Funktion, eine zufällige Karte zu generieren, wenn eine bestimmte Anzahl von Kacheln, eine Startkarte und die Art des zu verwendenden Gitters (dreieckig, quadratisch, hexadezimal) gegeben ist. Es gibt eine Reihe von Kacheln zurück,mapmit zwei zusätzlichen Eigenschaften:seed, die verwendete Startkarte undadjy*. Anfänglich,map ist gleich gesetztseed. Aus irgendeinem Grund,map.seed entspricht nicht demseed Streit. Stattdessen,map.seed gleichmap. Das habe ich herausgefundenseed wird geändert durchmap.push(). Ich habe versucht"use strict";mitarguments[1] anstattseedund verschieben Sie die Anweisungmap.seed=seed; herum (diese hatten keine Wirkung). Der Code hat keine Fehler in jsHint und wirft keine in die Konsole.

*adjy ist die Abkürzung für Adjazenz und 0 für Dreiecke, 1 für Quadrate und 2 für Sechsecke.

Ich habe ein vorbereitetjsFiddle und der Code ist auch unten:

console.clear();
var canvas=document.getElementById('canvas'),
    ctx=canvas.getContext('2d');
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
ctx.translate(canvas.width/2,canvas.height/2);
Array.prototype.equals=function(a){//array1==array2 is broken, create substitute
    return this.length==a.length&&this.every(
        function(e,i){
            return e instanceof Array?
                a[i]instanceof Array&&e.equals(a[i]):
                !(a[i]instanceof Array)&&e==a[i];
        }
    );
};
Array.prototype.includes=function(n){
    return this.some(
        function(e){
            return n instanceof Array?
                n.equals(e):
                n==e;
        }
    );
};
function Map(tiles,seed,adjy){
    if(!arguments.length)return [];
    if(arguments.length<2)
        if(tiles)seed=[[0,0]];
        else return [];
    if(arguments.length<3)
        if(tiles<seed.length)return adjy;//returns undefined
        else adjy=1;
    var map=seed,cdd=[],nos=//candidate tiles,Neighbors Of Square (or tile)
        [[[0,1,-1],[1,0,0]],//triangle
         [[0,1,0,-1],[1,0,-1,0]],//square
         [[0,1,1,0,-1,-1],[1,0,-1,-1,0,1]]][adjy];//hex
    function addAdj(p){//adds adjacent tiles to candidates (cdd)
        var c;
        for(var i=0;i<nos[0].length;i++)
            if(!cdd.includes(
               c=[p[0]+nos[0][i],p[1]+nos[1][i]])&&
               !map.includes(c))
                  cdd.push(c);
    }
    function pickR(){//pick a random tile
        var p=cdd.splice(
            ~~(Math.random()*cdd.length),1)[0];//~~ is the same as floor
        addAdj(p);
        map.push(p);//the line where the problem happens
    }
    seed.forEach(addAdj);
    while(tiles>map.length)pickR();
    map.seed=seed;
    map.adjy=adjy;
    return map;
}
function drawMap(map){
    if(!map.hasOwnProperty('adjy'))return void 0;
    function draw0(c){
        var x=c[0],y=c[1];
        ctx.beginPath();
        switch((x+y)%2){
            case 0:
                x*=5;y*=7;
                ctx.moveTo(x,y);
                ctx.lineTo(x-5,y+7);
                ctx.lineTo(x+5,y+7);
                break;
            default:
                x*=5;y*=7;
                ctx.moveTo(x,y+7);
                ctx.lineTo(x+5,y);
                ctx.lineTo(x-5,y);
        }
        ctx.closePath();
        ctx.fill();
    }
    function draw1(c){
        var x=c[0],y=c[1];
        ctx.fillRect(x*10,y*10,10,10);
    }
    function draw2(c){
        var x=c[0]*7,y=c[1];
        x+=y*3.5;
        y*=7.5;
        ctx.beginPath();
        ctx.moveTo(x,y);
        ctx.lineTo(x,y+5);
        ctx.lineTo(x+3.5,y+7.5);
        ctx.lineTo(x+7,y+5);
        ctx.lineTo(x+7,y);
        ctx.lineTo(x+3.5,y-2.5);
        ctx.closePath();
        ctx.fill();
    }
    switch(map.adjy){
        case 0:
            map.forEach(draw0);
            break;
        case 1:
            map.forEach(draw1);
            break;
        default:
            map.forEach(draw2);
    }
}
var board=new Map(37*6,[[-5,0],[5,0]],2);
console.log(board.seed);
drawMap(board);

Ich habe den gesamten Code eingefügt, falls das Problem nicht tatsächlich darin bestehtseed wird aktualisiert wennmap ist, obwohl meine Entwicklerkonsolentests dies vorschlagen.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage