I am using https://stackoverflow.com/a/1833851 to clone a function so that I can override it. For example:
Function.prototype.clone = function() {
var that = this;
var temp = function temporary() { return that.apply(this, arguments); };
for(var key in this) {
if (this.hasOwnProperty(key)) {
temp[key] = this[key];
}
}
return temp;
};
window.capture_list = [];
window.handler_clone = window.YAHOO.handleData.clone();
window.YAHOO.handleData = function(oRequest, oResponse, oData){
window.capture_list.push( {'oRequest': oRequest, 'oResponse': oResponse, 'oData': oData} );
return window.handler_clone( oRequest, oResponse, oData );
};
This appears to achieve the task, but I get a "Maximum call stack size exceeded error" in practice.
It seems like the clone method I'm using is recursing into itself somehow... I think I'm misunderstanding something about this clone implementation due to my limited js experience.
Any thoughts on where this recursion is, or how to better implement the clone for overriding? I'm really just trying to intercept the arguments passed to handler(). Thanks!
the handleData function:
window.YAHOO.handleData = function(oRequest, oResponse, oData) {
if (oData == null) {
oData = {}
}
oData.totalRecords = oResponse.meta.totalRecords;
return oData
};