0

class Snapshot { constructor(array) { this.array = array; } restore() { return this.array; } }

var array = [1, 2]; var snap = new Snapshot(array); array[0] = 3; array = snap.restore(); console.log(array.join()); // It should log "1,2" array.push(4); array = snap.restore(); console.log (array.join()); // It should log "1,2"

  • Hi, I think the attached answer is not similer to my question anymore! It is talking about obj and I am trying to ask about clasess... – Haider2013 Jan 16 '23 at 10:58
  • No, the thing you’re trying to clone is an Array, not a class. An Array is just an Object. The JSON approach, the `structuredClone` approach, etc. still work for Arrays. More specifically, there’s `.slice` for shallow copies of Arrays, too. So `this.array = array.slice();` in the constructor would be a start. – Sebastian Simon Jan 16 '23 at 11:20
  • Ok. Let me check and come back to you in case if any question. – Haider2013 Jan 16 '23 at 11:35

0 Answers0