stampit-org / stampit

OOP is better with stamps: Composable object factories.
https://stampit.js.org
MIT License
3.02k stars 102 forks source link

Using statics in an instance #308

Closed nachocab closed 7 years ago

nachocab commented 7 years ago

I was trying to replicate this value object tutorial using stamps and I'm wondering if there is a better way to use the grades static from the instance than by creating an explicit instance variable like this:

Grade = stampit({
  init(percentage, {stamp}) {
    this.percentage = percentage
    this.grades = stamp.grades // create explicit instance variable
    this.grade = this.grade(percentage)
  },

  statics: {
    grades: [
      {letter: 'A', minimumPercentage: 0.9, passing: true},
      {letter: 'B', minimumPercentage: 0.8, passing: true},
      {letter: 'C', minimumPercentage: 0.7, passing: true},
      {letter: 'D', minimumPercentage: 0.6, passing: true},
      {letter: 'F', minimumPercentage: 0,   passing: false}
    ]
  },

  methods: {
    grade(percentage) {
      return this.grades.find(x => percentage >= x.minimumPercentage) // access explicit instance variable
    }
  }
})

The whole point of using the static is to be memory efficient, so this seems suboptimal.

nachocab commented 7 years ago

Although, now that I think of it. I guess I could just do this:

Grade = stampit({
  init(percentage) {
    this.percentage = percentage
    this.grade = this.grade(percentage)
  },

  methods: {
    grade(percentage) {
      return Grade.grades.find(x => percentage >= x.minimumPercentage) // access explicit instance variable
    }
  }
})
Grade.grades = [
  {letter: 'A', minimumPercentage: 0.9, passing: true},
  {letter: 'B', minimumPercentage: 0.8, passing: true},
  {letter: 'C', minimumPercentage: 0.7, passing: true},
  {letter: 'D', minimumPercentage: 0.6, passing: true},
  {letter: 'F', minimumPercentage: 0,   passing: false}
]
danielkcz commented 7 years ago

Hey, @nachocab. since you are just "copying" reference, not a whole object, you don't really need to worry about memory efficiency.

More likely you should ask yourself why do you want to use statics in this case. It's paradigm from OOP, but you don't really need it here. In case you don't want anyone to change your grading table from the outside, it would be even recommended to store it just as a closure variable.

If you want your grades to be changeable for the stamp, you might as well go with deepConfiguration.

Edit: I wouldn't recommend the way in your second comment, it's rather messy in my opinion adding something on the stamp object like this.

nachocab commented 7 years ago

Thanks @FredyC! I'm having trouble trying to understand what conf and deepConf are used for (I'm reading the API, but I don't really get their purpose)

danielkcz commented 7 years ago

@nachocab Yea, it's rather undocumented feature. It's sort of similar to statics. You can check out package @stamp/configure I've made recently for some example.

koresar commented 7 years ago

I just realised that configuration and deepConfiguration were never properly explained. Thanks for bringing that in @nachocab !

Some more examples of how to use and access them can be found in Fan With Stamps. Episode 5.

nachocab commented 7 years ago

Thanks @koresar