This is what I would like to do
var myObject = {
prop: "872349827194721934798",
calcProp: (this.prop.length % 3),
method1: function () { return this.calcProp; },
method2: function () { return this.calcProp - 1; }
}
However it returns an error "this.prop is undefined".
Below works:
var myObject = {
prop: "872349827194721934798",
calcProp: function () {this.prop.length % 3;},
method1: function () { return this.calcProp(); },
method2: function () ( return this.calcProp() - 1;}
}
Now the usage of calcProp is myObject.calcProp()' and I want the usage to bemyObject.calcProp`. I know this will work.
var myObject = {
init: function () { this.calcProp = this.prop.length % 3; },
prop: "872349827194721934798",
calcProp: null,
method1: function () {
this.init();
return this.calcProp;
},
method2: function () {
this.init();
return this.calcProp - 1;
}
}
Is there a way to implement calcProp where the usage is Object.calcprop. Without calling an init().
EDITED - Additional Requirement
Note: Should have added to my requirements that I would like to try to stay away from modifying the properties outside of the initial definition. I do not want to do the following;
var myObject = {
prop: "872349827194721934798",
method1: function () { return this.calcProp; },
method2: function () { return this.calcProp - 1; }
};
myObject.calcProp = (myObject.prop.length % 3);
I would like to keep it all in the initial definition.
I would like to try to stay away from modifying the properties outside the initial definitionis not possible. JS is a scripting language and cannot self reference as it is being defined. You can sort of do this after, but you don't seem to want that.get, see my answer below