array sort changing curent and old variable

后端 未结 3 1748
无人及你
无人及你 2021-01-24 18:15

JS Source:

var c1 = [\"Saab\", \"Volvo\", \"BMW\"];
var c2=c1;
c1.sort();
document.getElementById(\"demo\").innerHTML = \'1 -> \'+c1+\'
2 -> \'+c2
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-24 18:54

    Just use slice(0):

    var c1 = ["Saab", "Volvo", "BMW"];
    var c2=c1.slice(0);
    c1.sort();
    alert(c1);
    alert(c2);
    

    Here a sample:

    http://jsfiddle.net/c3161930/

    Best regards, Felipe

提交回复
热议问题