Javascript passing arrays to functions by value, leaving original array unaltered

前端 未结 9 1864
不思量自难忘°
不思量自难忘° 2020-11-28 11:01

I\'ve read many answers here relating to \'by value\' and \'by reference\' passing for sending arrays to javascript functions. I am however having a problem sending an array

9条回答
  •  悲&欢浪女
    2020-11-28 11:40

    A variable pointing to an array is a reference to it. When you pass an array, you're copying this reference.

    You can make a shallow copy with slice(). If you want a full depth copy, then recurse in sub objects, keeping in mind the caveats when copying some objects.

提交回复
热议问题