Recently, I've been working on a code to construct a nested/recursive object/array tree. Upon implementing getters/setters to the object, I noticed that I can only access properties in the root of the tree using the "_" prefix. This becomes apparent around line 89 of the code, near where the console.logs begin.
// In this code snippet, my goal is to establish a single "template" object definition that can be utilized to build a tree structure consisting of branches and leaves of the same object type.
// Additionally, functions have been created to retrieve/set values for any properties of any leaf by utilizing an array representing the leaf's position within the tree.
// Define Object Template for Branches/Leaves
class objTemplate {
constructor(SpineName, Width, Height) {
this.SpineName = SpineName;
this.Width = Width;
this.Height = Height;
this.Area;
this.Leaves = [];
}
get SpineName() {
return this._SpineName;
}
set SpineName(value) {
if (value.length === 0) {
console.log("Sline Name Is Required.");
return;
}
this._SpineName = value;
}
// More getter/setter methods...
}
// Function to Add Leaf Objects to Tree
function pushLeaf(Position, Tree, NewLeaf) {
Position.reduce((val, prop) => { return val.Leaves[prop]; }, Tree).Leaves.push(NewLeaf);
}
// Other utility functions...
// Initial Tree Creation
var objTree = Object.assign(Object.create(Object.getPrototypeOf(objTemplate)), new objTemplate("Root", 4, 5));
// Adding Leaf Objects to Root Branch
pushLeaf([], objTree, new objTemplate("Oreo", 3, 4));
pushLeaf([], objTree, new objTemplate("Jupiter", 7, 3));
// Additional nesting of objects...
console.log(`Root Object Spine Name: ${getValue([], objTree, "_SpineName")}`); // Use "_" prefix when accessing properties with getters/setters
// Further console logs...
After integrating getters/setters, the necessity of using the "_" prefix for property access became apparent. While I could continue this practice uniformly across all positions in the tree, I'm unsure of whether this is considered standard or if there's a better approach altogether. Any guidance on this matter would be greatly appreciated!