dalecosp;11025715 wrote:Gee, I hope you've asked this someplace else. Aren't you the JS guru around here? 😉
Well, I started this little project to learn (more) javascript 🙂
I was actually certain that Weedpacket would have an answer to this... And voilà 🙂
Weedpacket;11025721 wrote:A set of accessor functions of some sort?
function Planet() {
this.set = {maxpop: function(val) { maxpop = val; }};
Brilliant. A setter object handles this perfectly. And also, the same technique made it possible to do away with the conditional branch to call either getNullTerminated when setting Planet.name and reverseByte for the others!
Thanks 🙂
For reference, it now looks like this
var indices = {
'name' : {
'start' : 0,
'end' : 12,
'extractor' : getNullTerminated
},
'maxpop' : {
'start' : 30,
'end' : 31,
'extractor' : reverseByte
},
etc : {}
}
this.set = {
name : function (v) { name = v; },
maxpop : function (v) { maxpop = v; },
etc : {}
}
// this.set[x] => this.set.name, this.set.maxpop
// and now also calling indices[x].exctractor for indices.name.getNullTerminated(), indices.maxpop.reverseByte()
for (x in index) {
this.set[x](indices[x].extractor(pd.substring(index[x].start, index[x].end)));
}
And while it may perhaps initially look as if it's an equal amount of typing to get those setter functions in place as typing it out explicitly... First off, I need those setters anyway. Besides, now I can do away with the former setter functions which could not be called the way I needed them: Planet.setName(), Planet.setMaxPop() etc.