Exploring a new concept. Consider an object like:
T = {
a: 2,
b: 9,
c: {
a: 3,
d: 6,
e: {
f: 12
}
}
}
The goal is to modify it so that every value that is an object becomes the same object, with the parent object as prototype.
For example, we should get the following results:
> T.c.b
9
> T.c.e.b
9
> T.c.e.a
3
> T.c.c.c
{a: 3, d: 6, e:[Object]}
A set of functions has been developed already, which are almost working as intended:
function chainer(object) {
for (const key in object) {
if (object[key] !== null && typeof (object[key]) === 'object') {
let Constructor = function () {
};
Constructor.prototype = object;
let objectValue = {...object[key]};
object[key] = new Constructor();
for (const savedKey in objectValue) {
object[key][savedKey] = objectValue[savedKey];
}
}
}
}
function chain(object) {
chainer(object);
for (const key in object) {
if (object[key] !== null && typeof (object[key]) === 'object') {
chainer(object[key]);
}
}
}
While the above example works fine, there seems to be an issue when trying the following:
T = {a:4, g:{g:{g:{g:{g:{g:{g:{}}}}}}}
This yields unexpected results:
> T.a
4
> T.g.a
4
> T.g.g.a
4
> T.g.g.g.a
undefined
> T.g.g.g.g.a
undefined
It is puzzling why it stops working at a certain point; could there be some unknown limitation causing this?
Feeling a bit stuck and running out of ideas, any suggestions?