OK, I doubt this is a unique situation, so either someone has done this, or someone has decided this isn't possible, at least in the way I am asking.
I have 2 prototyped variables (functions), one is the parent, the other is a helper. What I'd like to do is reference the parent from within the helper such that each instance of each parent can utilize the helper while allowing the helper to essentially extend the parent. I'm using jQuery and javascript.
Here's a simplified example:
var Parent = function(identity) {
this.identity = identity;
this.group = $(this.identity);
this.helper = new Helper(this);
return this;
};
Parent.prototype = {
_setup : function(dolisten) {
dolisten = dolisten || false;
var that = this;
that.group.each(function(i) {
if ($(this).data('key') !== 'undefined') {
that.elems[i] = new Object();
that.elems[i].key = parseInt($(this).data('key'));
// build out rest of elems object
}
});
if (dolisten) {
this._listen();
}
},
_listen : function() {
var that = this
$('body').on('click tap', that.identity, function() {
that.helper._showbusy(this, true);
// do some other stuff
}
}
};
var Helper = function(reference) {
this.reference = reference;
};
Helper.prototype = {
_showbusy : function(elem, isbusy) {
console.log(this.reference);
// do some stuff to elem
},
};
This works but creates an essentially infinite reference to Parent from within its own .helper node. So, without actually passing the parent every single time (hoping to create a single point of reference just once, the above seems to cause a reference "loop"?), can this be done elegantly?
In the end, what I would like is a simple:
var obj = new Parent('.myClass');
console.log(obj);
// result : Parent { identity:'.myClass', group: [Object object], helper: {reference : *Parent without helper key included, which is where the looping seems to occur}, etc...
EDIT: For the time being, I'm resorting to passing in the specific keys of the parent that are needed by the helper. This prevents the helper from infinitely cascading into copies of the parent and itself, but a more elegant solution, where helper has access to the entire parent instead of just the specific keys passed, without cascading helper's reference to parent to become parent -> helper -> parent -> helper -> parent -> etc...