Initially, I believed that objects only inherit from their parent to child, but the code I wrote reveals an unexpected inheritance among "siblings". This made me question why all the objects in my array testObj (which are children of myObj) share the same data. The code I wrote was purely for learning purposes with no particular goal in mind.
// Defining my prototype object
const myObj = {
myArray: [],
declaration: ""
}
// Creating an array of objects
let testObj = [];
// Populating my object
for (i=0; i < 4; i++) {
testObj[i] = Object.create(myObj);
testObj[i].myArray.push("content of array " + i);
}
// Printing the data
testObj.forEach((eleA,idxA) => {
console.log("\nobj number "+idxA);
eleA.myArray.forEach((eleB,idxB) => {
console.log(eleB);
});
});
Console Output:
obj number 0
content of array 0
content of array 1
content of array 2
obj number 1
content of array 0
content of array 1
content of array 2
obj number 2
content of array 0
content of array 1
content of array 2