Skip to content
Advertisement

How to return an Immutable object from a factory function in JavaScript

I have a basic function accepts Temperature data as an argument and then perform a simple temperature conversion operation on the data How can we perform the same functionality using without mutating the object? i.e, the function should not mutate the argument passed in, it should rather return a copy

JavaScript

I have some helper functions to help with conversion

JavaScript

the args object looks like so :

JavaScript

Advertisement

Answer

so the functions will look like this, and that solves the issue

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement