I am trying to extend an object, and I want to modify a method of the first, not to override it. I don’t think this is clear so here is an example:
var object1 = {
whatever : function(){
console.log('first object method');
}
}
var object2 = {
whatever : function(){
console.log('second object method');
}
}
var object = $.extend(true, object1, object2);
object.whatever();
This example will output second object method, but what I want is that it ouput first object method and second object method.
Is there a way to do this?
Javascript uses prototypal inheritance, so you can make object2 inherit object1’s properties and methods by doing
this means that object2 will inherit all the properties and methods of object1, and any overwritten methods can be accessed at
object2.prototype.method()so
parent::whatever();is equivalent tothis.prototype.whatever();when calling from the scope of object2.