Weedpacket's reply in another thread lead me to a couple of followup quesitons not really related to that thread
Initially I was mainly curious about wether it's considered bad practice to use getter / setter properties with anything but some kind of derived values. See first code snippet below for example of using it to access directly underlying property (and is that bad).
As I started to try using them, I quickly ran into some problems, and I can't find any information on this. It seems I can only use get/set when using an object initializer list, i.e.
var ob = {
storedVar : 4,
get varGetter() { return this.storedVar; }
}
ob.storedVar; // storedVar is public, so value is 4
/* Here's the use of getter property to return directly underlying property */
ob.varGetter; // calls varGetter() which returns this.storedVar, so value is 4
Here, I'm also curious about control over visibility when using object initializer lists. Can they somehow define private and privileged properties? I'm guessing not, but perhaps someone has more info on why. If nothing else, it'd probably make my first question into a non-issue, since you wouldn't define a public getter for a public property. You'd just access the property to begin with.
Then, as I tried using the getter / setter properties when using a constructor function, I failed... Is it at all possible, and if so - how?
function ob() {
var storedVar = 4;
/* privileged function which is publicly accessible and in turn has private access */
this.privilegedGet = function() { return storedVar; }
/* What would the syntax for making use of get be in this case? Is it even possible?
* The following gives a "missing ; before statement, pointing to the v in varGet
* Besides, varGet would be private if it follows the same scope rules as writing
* function varGet() { }
* get varGet() { return storedVar; }
*/
/* And same question again.
* this.alsoPrivileged = function() { return varGet; }
* would define the privilieged function alsoPrivileged.
* But using it with get gives the same error message,
* missing ; before statement, but now indicates the opening {
* this.varGet = get() { return storedVar; }
*/
}
/* Trying to add a public getter as you'd add a public function
* ob.prototype.funcName = function() {}
* also gives the same error message,
* missing; before statement
* while also indicating the opening {
*/
ob.prototype.asPublic = get() { return storedVar; }
/* And while it might seem silly to add a static getter property
* I had to try. This also gives missing ; before statement, indicating first {
*/
ob.asStatic = get() { return 4; }
Once again, I'm guessing not, but I'd rather be sure. And I still like an answer to why.