O'Reilly logo

Mastering Javascript Functional Programming : Become skilled with Functional Programming in JavaScript by applying Javascript concepts to real world development problems by Federico Kereki

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating objects

This kind of method can also be applied to more common requirements, such as modifying an object. This is a very good idea for, say, Redux users: a reducer can be programmed that will receive the old state as a parameter and produce an updated version with the minimum needed changes, without altering the original state in any way.

Imagine you had an object as follows:

myObj = {    a: ...,    b: ...,    c: ...,    d: {        e: ...,        f: ...,        g: {            h: ...,            i: ...        }    }};

If you wanted to modify myObj.d.f, and wanted to do it in a persistent way, you would create a new object, which would have several attributes in common with the previous object, but would define new ones for the modified ones. See figure 10.6:

Figure 10.6. A persistent way of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required