Extend prototype function without overwriting

I need to fix a bug in the save function of the Parse.Object library. However, when I try to call the original save function in my overwritten prototype, it is recursively recursive until the stack overflows!

 Parse.Object.prototype.save = function (arg1, arg2, arg3) { fixIncludedParseObjects(this); Parse.Object.prototype.save.call(this, arg1, arg2, arg3); // endless loop }; 

How can I change the infinite loop line to call the original function created by parsing?

Thanks!

+8
javascript prototype
source share
3 answers

Try the following:

 (function(save) { Parse.Object.prototype.save = function (arg1, arg2, arg3) { fixIncludedParseObjects(this); save.call(this, arg1, arg2, arg3); }; }(Parse.Object.prototype.save)); 
+18
source share
 Parse.Object.prototype.save = function (save) { return function () { fixIncludedParseObjects(this); //Remember to return and .apply arguments when proxying return save.apply(this, arguments); }; }(Parse.Object.prototype.save); 
+4
source share

Similar to accepted answer, but maybe a little easier to understand

 var originalSaveFn = Parse.Object.prototype.save; Parse.Object.prototype.save = function(arg1, arg2, arg3) { fixIncludedParseObjects(this); originalSaveFn.call(this, arg1, arg2, arg3); }; 
0
source share

All Articles