1

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?

1 Answer 1

4

Not jQuery, but some other libraries/scripts add bind to Function.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 bind property 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:

Prototype not loaded
jQuery not loaded
function bind() { [native code] }

Example with page with latest jQuery, output on Chrome:

Prototype not loaded
jQuery found: 1.6.2
function bind() { [native code] }

Example with page with latest Prototype, output on Chrome:

Prototype found: 1.7
jQuery not loaded
function bind(context) { if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; var __method = this, args = slice.call(arguments, 1); return function() { var a = merge(args, arguments); return __method.apply(context, a); } }

From your example, you're not loading the latest Prototype, but something is overwriting Function.prototype.bind.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You're absolutely right - I found a script on my page that is replacing bind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.