If I browse to about:blank, open the script console and type the following:
var x = function() {
console.info(this.toString() + ' -- ' + arguments.length.toString());
};
x.bind;
The response shows that x.bind is implemented in native code:
function bind() { [native code] }
However, when I pull up the script console on a page of my web app and execute the same statements, it looks like x.bind is not natively impplemented:
function (a){var b=this;return function(){b.apply(a,arguments)}}
What would cause this implementation to switch like this? Is there potentially something I’m setting in my javascript that could cause this? I’m using jQuery on the page – would that have an impact?
Not jQuery, but some other libraries/scripts add
bindtoFunction.prototype, and some of them do it without checking if it’s already there, happily overwriting the native implementation. I assume you must be using some other script (besides jQuery) on the page, and that other script (whether it’s a jQuery plug-in or whatever) is overwriting without checking.I’ve just tested in Chrome, and regardless of whether I have jQuery loaded or not, from within an actual page looking at a function’s
bindproperty shows the native code marker. (In contrast, if I load the latest Prototype, it overwrites Chrome’s native version with its own.)Example with page with no libraries, output on Chrome:
Example with page with latest jQuery, output on Chrome:
Example with page with latest Prototype, output on Chrome:
From your example, you’re not loading the latest Prototype, but something is overwriting
Function.prototype.bind.