I’m looking to be able to extend the function prototype in javascript (that is to say, add a function to all functions).
The purpose is to convert it into a format for exchange between another language (namely ruby, which can only communicate through strings [in this particular setting]). I’ve already got a system so I can pass around other types by defining a to_js method on ruby objects and a to_ruby method on javascript objects, like so
Number.prototype.to_ruby = function () { return this.toString(); }
This is working for everything else I want it to, but not for functions. I can get it to work in chrome by doing the following:
_empty = function() {};
_empty.__proto__.to_ruby = function () {
return 'JSFunction.new(' + this.toString().to_ruby() + ')';
};
But this does not work in IE (which is a requirement of the system).
I know I have an object somewhere keeping a track of functions by an ID or similar, but I can’t guarantee that they will be used with the same instance that created them.
All else failing I could just write a function to special case deal with it (ie, isFunction(instance) ? fn_to_ruby(instance) : instance.to_ruby(), but I’d rather keep this model if possible.
__proto__is not a standard property. Try this approach: