Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Overwriting the call function

I'm trying to overwrite the builtin Function.prototype.call function to log function calls for debugging. In the browser I would do something like


(function() {    
var call = Function.prototype.call;
Function.prototype.call = function() {
log(this, arguments);
return call.apply(this, arguments);
};
}());

I seem to be missing something essential about the way the API works, since the above code basically works but it doesn't seem to have access to 'this' and 'arguments' variables? Has somebody tried something like that and can point me in the correct direction?

October 26 (6 years ago)
The Aaron
Pro
API Scripter

I haven't tried this, but I'm curious about it.  Do you get an error, or just don't get the expected behavior?  It's possible the call property is set to be non-configurable.  Try doing something like:

const descriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'call');
log(descriptor);

And see if the configurable property is false, or if something else leaps out at you.  Also, you could create the function as it's on variable, do the set, and then check if it worked:

(function() {    
const call = Function.prototype.call;
const myCall = function() {
log(this, arguments);
return call.apply(this, arguments);
};    Function.prototype.call = myCall;    if(myCall === Function.prototype.call){     log('Setting myCall worked.'); } else { log('ERROR: setting myCall FAILED!!!'); }
}());