Alan2
Alan2

Reputation: 24552

What does console.log.bind(console); do?

I have this in an application I am working on:

    $rootScope.$on('$stateChangeError',
        function (event, toState, toParams, fromState, fromParams) {
            console.log.bind(console);
        });

Can someone give me advice on what it does? I have no idea why it was added but now I am looking at it and think I should at least know what it does.

Upvotes: 0

Views: 1092

Answers (2)

mgilson
mgilson

Reputation: 309821

It does nothing, at least not anything useful ...

Sometimes you might see something like:

z = x.y.bind(x)

In this case, z is a function that executes the function x.y with the this argument set to x. In your case, since the return value of bind is never used, the statement is just creating a function that never gets used. You'll also see this frequently passed to callbacks. As a stupid example:

var foo = function(a, callback) {
    callback(a);
};

foo(a, b.c.bind(b));

This is the same thing as calling b.c(a)

Upvotes: 0

deceze
deceze

Reputation: 521984

In this particular case it does absolutely nothing of value. I'd think some refactoring went wrong, and what it was actually supposed to do was this:

$rootScope.$on('$stateChangeError', console.log.bind(console));

This attaches the console.log method as event handler, which will simply cause it to log every invocation of the event. Due to the way this is bound at calltime, .bind(console) is necessary to preserve the context correctly.

Upvotes: 3

Related Questions