jQuery has nothing to with JS inheritance, though you might use some Callbacks
objects for organising your callbacks.
You could do something like
Parent.prototype.fire = function(args) {
if (this.hasOwnProperty("callbacks")) {
for (var i=0; i<this.callbacks.length; i++)
this.callbacks[i].call(null, args);
}
var proto = Object.getPrototypeOf(this);
if (proto && "fire" in proto)
proto.fire(args);
};
Now, everything that inherits from Parent.prototype
can use this method which checks for a "callback" array on the current instance, executes them and then recursively walks up the prototype chain until there is no fire
method.
function Child() {
this.callbacks = [console.log.bind(console, "Child level:")];
}
Child.prototype = new Parent;
function GrandChild() {
this.callbacks = [console.log.bind(console, "GrandChild level:")];
}
GrandChild.prototype = new Child;
var gc = new GrandChild;
gc.fire("something");
However, I usually recommend not to use new
for creating inheritance chains. Depending on your application structure, it might work, but know what you are doing. You could easily get lost in inheritance of nested objects, and also you might need to avoid creating local variables in your constructors.