3 ms·
> In an immutable world, the coupling of functionality and data is extremely difficult to use because any functionality would have to be rebuilt any time the fa
by victorNicollet 11y ago
> In an immutable world, the coupling of functionality and data is extremely difficult to use because any functionality would have to be rebuilt any time the facts change.
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.