Currently, a project I'm working on requires an object type called Chain
that generates another object type called Link
which is exclusively used by the former.
In my past projects, I had embedded the constructor and prototype of Link
within the constructor for Chain
. However, this time around, I started to question whether this approach was optimal, as it seemed like I was defining Link
separately for each instance of Chain
.
To provide some context, here is 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 then started contemplating whether I should keep the definition of the Link
constructor and prototype outside in the main scope similar to how I did with
Chain</code, or if I should utilize the <code>Chain
prototype to define Link
and keep it there.
Unfortunately, I couldn't find much guidance on best practices for such scenarios. While I suspect that my initial approach might not be ideal, I am uncertain about the alternatives either.
Hence, I pose the question: What is the most efficient and sensible way to handle this situation?