We simulate classes in JavaScript using this kind of structure:
OurApplication.TableViewer = Class.extend ({
init: function() {},
viewLogic: function() {},
logLogic: function() {}
}
Class.extend is a function that runs 'init' like a constructor and otherwise simulates classes in a somewhat Java-like way. I think this is similar to what Crockford calls 'classical inheritance' and this general approach (if not the exact syntax) seems to be somewhat commonly used/recommended, so I'm hoping that it's not controversial.
My question is, how data that is global to the 'class' should best be passed between functions within the same 'class';
Method A:
OurApplication.TableViewer = Class.extend ({
init: function() {
this.foo = 'bar';
viewLogic();
logLogic();
},
viewLogic: function() {
document.write(this.foo);
},
logLogic: function() {
console.log(this.foo);
}
}
Method B:
OurApplication.TableViewer = Class.extend ({
init: function() {
viewLogic('bar');
logLogic('bar');
},
viewLogic: function(foo) {
document.write(foo);
}
logLogic: function(foo) {
console.log(foo);
}
}
I couldn't find a lot, but I did find this post which I think is the same topic but for Java. I would say it is barely favoring 'method A', but it's not at all clear.
Obviously there are some situations where it makes sense to pass data as a parameter (eg inside a loop where the value being passed changes each time; you could alter 'this.foo' to be the new value each time, but that seems awkward).
Any opinions/insights appreciated.