[Javascript] Data ownership, avoid accidently mutation
When implementing the store partten, we need to be careful about mutation. class DataStore { private lessons: Lesson[] = []; private lessonsSubject = new SubjectImplementation(); lessonsLists$: Observable = { subscribe(obs) { this.lessonsSubject.subscribe(obs); obs.next(lessons); }, unsubscribe(obs) { this.lessonsSubject.unsubscribe(obs); }, }; initializeLessonsList(newList: Lesson[]) { this.lessons = _.cloneDeep(newList); this.lessonsSubject.next(lessons); } addLessons(newLessons) { this.lessons.push(_.cloneDeep(newLessons)); // make a deep clone this.lessonsSubject.next(this.lessons); } }