A project I'm working on currently has called for an object type Chain
that itself produces a type of object Link
that only it uses.
In previous projects, I had nested the object constructor and prototype of Link
inside the constructor for Chain
, however, I began to wonder this time if this was the best way to do this as I would actually be appearing to set up each Chain
with its' own definition of Link
every time.
For clarity, this was the original code I had:
var Chain = function() {
var self = this;
var Link = function(key) {
this.prop = key;
};
Link.prototype.attach = function(args) {
for (let value of args) {
this[value.prop] = value.value;
}
};
self.links = [];
}
Chain.prototype.add = function(key) {
this.links.push(new Link(key));
}
I have then begun wondering if I should just store the Link
constructor and prototype out in the wild like I have with Chain
(for the record, it's not quite out in the wild, it's actually contained within a object) or whether I should use the prototype of Chain
to define Link
and keep it there.
I've failed to find much information on best practice in this situation, and while I strongly suspect my first way of doing things is not the best/right way; I'm not sure if my alternatives are either.
So I ask, what is the best practice/most efficient/sensible way to do this?