This is called monkey patching. Although I don't recommend to go over the board with this, I'll start from a simpler example. Let's assume you want to change console.log of the browser, then you do:
console.log = (function(consoleLog){
return function(a){
consoleLog(a+"_HaHa")
}
}(console.log));
console.log("hoho") //hoho_HaHa
The idea of a monkey patch is to:
modify the behavior of a function without touching its internals.
Moving from the example you have given, I will create a similar demonstration (it can be written much shorter but just to keep resemblance to your original example):
//PREPARATION CODE
var obj = {a:0},
obj2 = {b:-1},
fabric = {util:{object:obj}};
obj.toObject = function(){this.a++};
function extend(obj, id) {
console.log("called");
obj2.b++;
obj &&
(obj.toObject = (function(toObject) {
return function() {
return fabric.util.object.extend(toObject.call(this), {id: id});
};
})(obj.toObject));
}
obj.extend = extend
//CALL EXTEND ONCE
extend(obj,"x") //id doesnt matter here
//NOW CALL obj.toObject() to increment both a and b
obj.toObject();
obj.a //1
obj2.b //1
So we have modified the original toObject method to do something else without modifying what it originally did.
Once last word about iief and monkey patches is that if you are using them in your daily routines for lazily computing values or to get values that will be available at some point during runtime it is OK.
But if you are constantly using monkey patches to modify existing behavior of many predefined objects you are getting yourself into a ever tangling cat-mouse game. Duration of the game usually depends on the how well you remember what have you done in the past.
It is akin to playing with fire, though it can actually be useful and fun, unless you don't know when to stop.