A
A
Adel Khalitov2019-04-04 18:37:50
Angular
Adel Khalitov, 2019-04-04 18:37:50

Why is the angular service cached?

I am making a calculator. Now I'm trying to separate the logic so that it's easier to edit and extend my calculator.
Faced with one problem, it should not arise in printsepi. I can't find the reason.
Example:
There is a main service that considers something
first.service.ts

basefunction(obj) {
   var result = this.childrenservice.somefunction(obj);
   console.log(result)
}

children.service.ts
somefunction(obj) {
    obj['Inventory'] = this.Inventory; //делает копию объекта
    for (let y in details.Inventory) {
      details.Inventory[y]['count'] = 1; // Создает елемент равным 1
    }
   details.Inventory['ItogMonth'] = 123123; // Добавляем какой то итог.
}

The first time the function is called, there are no problems, the elements ItogMonth and count are created for the first time, etc.
But when the child function is called again, it already has an ItogMonth element and an error occurs,
ERROR TypeError: Cannot create property 'count' on number '123123'

although obj does not contain ItogMonth. I conclude that it is cached somewhere in RAM. Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-04-04
@SaveLolliPoP

He does not make a copy of the object for you, but receives a link to it. In js, in this case, as you write, there is no cloning, a link to the original object is simply put there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question