Code Monkey home page Code Monkey logo

Comments (2)

Gozala avatar Gozala commented on May 20, 2024

@thom-nic with Record types you could define Person type like in the example below. Defined records will have all the getters for fields defined:

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 I personally like to use plain functions:

fullName = ({first, last}) => `${first} ${last}`

I used to put such functions as a static methods of the Record constructor Person.fullName = fullName just for grouping, but I ditched that practice and tend to group functions into modules instead.

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:

  1. I like to keep data separate from operations that can be performed on it. That way all operations are equal no first class second class.
  2. Functions are more convenient for functional compositions and with high order functions like .map
    .filter etc...

Ultimately I think it's really just matter of personal preference and does not really matter.

from typed-immutable.

Gozala avatar Gozala commented on May 20, 2024

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 .asMutable() API.

from typed-immutable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.