I usually use :

JSON.parse(JSON.stringify(objectToClone))

But it doesn't work well when your object have cyclical references. And the good news is that it doesn't always depend of you.

In the last issue I had, one of my object had a member called 'idSetInterval'. Its value was the value returned by the setInterval() function (the function used to trigger another function call every x milliseconds). This setInterval() function is supposed to return an integer, but in nodejs it returns an object that is part of a doubly linked structure (my code was working pretty well in the browser but not in the backend):

Example of value returned by setInterval() in NodeJS:

Timeout {
  _idleTimeout: 100,
  _idlePrev: [Timeout],
  _idleNext: [Timeout],
  _idleStart: 1742197,
  _onTimeout: [Function],
  _timerArgs: undefined,
  _repeat: 100,
  _destroyed: false,
  [Symbol(refed)]: true,
  [Symbol(kHasPrimitive)]: false,
  [Symbol(asyncId)]: 338,
  [Symbol(triggerId)]: 5
}

So JSON.stringify doesn't know how to manage cyclical references. I managed this with some hacks, but with doubly cyclical references it is harder.

Do you have a better or simpler method to clone objects in javascript ?

 

Quel est votre pire souvenir depuis que vous développez ?

En ce qui me concerne, il s'agissait d'un projet sur lequel les données étaient repartient sur 3 BD différentes qui n'étaient pas interconnectés. Ces BD étaient sous le même SGBD. Il fallait donc réunir les données de ces 3 BD dans une seule BD en centrale.

Cette tâche m'a donné des sueurs froides et direct après j'ai décidé de suivre un cours sur les SGBD

OSS Cameroon launches a survey about developers. Feel free to participate


Hello Guys ! We created a survey because we are planning a serie of tutorials and live coding. This survey help us know what interest you. Please take few minutes to answer and share it in other tech groups you belong to. Thank you in advance 👍🏽

https://forms.gle/YH9nbPxwn1BCCpQB8

Hackaton du port autonome de Kribi, toutes les informations sont disponibles ici


- Règlement du concours Hackaton PAK 2021: http://www.pak.cm/fr/node/1205
- Formulaire d'inscription: https://forms.office.com/Pages/ResponsePage.aspx?id=dpM4iZ0wpkKZSgGm3WEd0iwS1tJKkaVKkMnDbTpNQEZUQ1dZT0tOUk9XNE0yWFc5VlZKM1lJUjlPNyQlQCN0PWcu
- Information complémentaire - ÉTUDE SUR LES TRANSPORTS MARITIMES 2018: https://unctad.org/system/files/official-document/rmt2018_fr.pdf