-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Adding functions to types for derived data? #7
Comments
@thom-nic with const Person = Record({first: String, last:String})
const bob = Record({first: 'Bob', last: 'Fish'})
bob.first // => 'Bob'
bob.last // => 'Fish As of computed field like fullName = ({first, last}) => `${first} ${last}` I used to put such functions as a static methods of the Record constructor You could also define them as methods on the prototype if you like: const asMethod = f => function(...args) => {f(this, ....args)}
Person.prototype.fullName = asMethod(fullName) That is sometimes useful if you want a polymorphism behavior, although for that I still tend to use method library or if I don't want pull in extra library then this pattern: const slice = (value, from, to) => value.constructor[$slice](value, from, to)
$slice = slice.symbol = Symbol.for('mylib/slice')
MyType[slice.symbol] = (myType, from, to) => /*... */
MyOtherType[slice.symbol] = (myOtherType, from, to) => /*... */ I prefer functions because:
Ultimately I think it's really just matter of personal preference and does not really matter. |
Oh and I don't personally like property getters for computed properties as I think they live an impression of being regular property while under the hood they do run code that may cause side-effects throw exception etc.. I much rather prefer to be able to distinguish computation from property access while reading a code without having to digg into implementations. P.S. Record do use getters under the hood though, primarily so that they would throw exceptions at you if you try to mutate them and to make assignment possible on |
I'm somewhat new to react/redux and have been struggling with how to handle "derived data." A trivialized example is given raw data like
what is the appropriate way to define
person.fullName
? Or should I just havepersonUtils.fullName(person)
? It seems if I were to use typed-immutable to define the structure of my data I could also define methods/ property-getters to define derived data.Good idea/ bad idea?
The text was updated successfully, but these errors were encountered: