This just isn't functional
codewords.recurse.com
codewords.recurse.com
This is wrong. The problem is lack of support for immutable data in Javascript, not with immutable data in itself. Consider the OCaml equivalent:
class person name = object
val _name = name
method name = name
method greet other =
"Hello " ^ (other # name) ^ ", my name is " ^ _name
method set_name name =
{< _name = name >}
end
This creates an immutable person class. Calling 'set_name' on a person creates a new person and the methods of that person use the new value. You can even use inheritance : class whistleblower secrets name = object
inherit person name
val _secrets = secrets
end
let bradley = new whistleblower (...) "Bradley"
let chelsea = bradley # set_name "Chelsea"
The new person is an instance of "whistleblower" and has the same value for "_secrets" as the original object.By this author's logic, his own implementation is no good, because what if someone comes along and writes Protocol = "foo" in global scope? There's a difference between writing defensive code to prevent mistakes anyone could easily make, and trying to push your pet implementation forward through ridiculous hypotheticals.
Also, no mention of ES6 fat arrow functions and no mention of Immutable.js indicate that maybe this guy isn't interested in solving problems as much as he is in promoting his personal style.
Introspecting into this code seems very complicated and could make future refactors painful.
That is why I use deep-freeze. I also have a mutate/copy function that only changes from the changed property up to the root. Everything that can stay frozen as the original object will stay frozen.
var sonalisTemp = get(sonali, "name"); // sonalisTemp === 22
I think you meant
var sonalisTemp = get(sonali, "temperature"); // sonalisTemp === 22