Is there a better method for merging two objects and calling parent functions?
var a = {
a:1,
myFunction:function(){
console.info("a");
}
}
var b = {
b:2,
myFunction:function(){
console.info("b");
this.callParent();
}
}
var obj = {};
$.extend(obj, a);
$.extend(obj, b);
b = obj;
b.superclass = a;
wrapping all functions so "this.callParent" is present
function wrap(func, parent) {
return function() {
this.callParent = parent;
var result = func.apply(this, arguments);
return (this.callParent), result;
};
}
with an iteration such as:
$.each(b, function(key, value){
if(typeof value == "function" && b.superclass && b.superclass[key]){
b[key] = wrap(value, b.superclass[key]);
}
});
with an output "b -> a":
b.myFunction();
$.extend();would not be related to inheritance in JavaScript. All$.extend()does is merge 2 objects it does not care much for theprototypeor theprototype.constructornor would$.extend(objA, objB)causeobjA instanceof objBto be true. Saying JavaScript inheritance with jQuery is very misleading.