non mutating function, no side affects

PHOTO EMBED

Tue Feb 09 2021 06:55:27 GMT+0000 (Coordinated Universal Time)

Saved by @noobj

const foo = {
  ONE: 1,
  TWO: 2
};
const mapMultiplier = (params, multiplier) => {
  // Only shallow clone works, deep clone is expensive and need other mechanisms
  const paramsCopy = { ...params }; // Object.freeze(params); //  Object.assign({}, params)
  paramsCopy.ONE = paramsCopy.ONE * multiplier;
  paramsCopy.TWO = paramsCopy.TWO * multiplier;
  return paramsCopy;
};
console.log(mapMultiplier(foo, 2));
console.log(mapMultiplier(foo, 3));
console.log(mapMultiplier(foo, 4));

// Value of foo { 'ONE': 1, 'TWO': 2 }
console.log('foo: ', foo);
content_copyCOPY