3 ms·
One thing I don't get it is people who would say you must choose between objects and functions, or that FP is "better" than OOP. Objects have function-valued p
by galaxyLogic 7y ago
One thing I don't get it is people who would say you must choose between objects and functions, or that FP is "better" than OOP.
Objects have function-valued properties called methods. So Objects can't do without functions.
Functions can return either a) atomic values like strings and numbers OR b) Objects. Note that in JavaScript Functions are Objects too.
So I hope no-one is seriously suggesting that FP can do without Objects, by saying that functions should only return atomic values.
- archarios 7y agoThe problem here is that this conversation is made complicated by the fact that plain objects in JavaScript are not what is really being talked about when people say the term OOP. So to clarify, stateful classes would be what I am talking about when I use that term. The foundation of OOP is based on tying state with functions in what are deemed "Objects". This implicitly makes all methods in an Object that depend on or modify an Object's internal state impure functions. And the purpose of most Objects methods are for the purposes of doing those things. Basically if you use stateful classes that encapsulate state and modify their own internal state with functions you are not doing functional programming. These two paradigms are in fact at odds and not compatible.
- galaxyLogic 7y ago> This implicitly makes all methods in an Object that depend on or modify an Object's internal state impure functions Note that you can have methods which do not depend on mutable state. In ES6 you can FREEZE objects, which you could do in the constructor. Freeze the object after having set its "instance variables". Now all methods of that object are "pure functions". So you can choose. Choose to create pure functions or impure. Creating a new immutable instance really is equivalent to creating a set of immutable ('pure' if you like) functions.