Closed nachocab closed 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}
]
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.
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)
@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.
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.
Thanks @koresar
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:The whole point of using the static is to be memory efficient, so this seems suboptimal.