JavaScript confuse javascript bind function

From MDN:

The bind() function creates a new function (a bound function) with the same function body (internal Call attribute in ECMAScript 5 terms) as the function it is being called on (the bound function's target function) with the this value bound to the first argument of bind(), which cannot be overridden.

Basically this means that you can't call bind on an already bound function.

In your example, you'd have to do:

func2 = someuser.func.bind(someuser);
Answer:1

I think what you want is a weak bind:

function weakBind(functable, context) {
    var GLOBAL = this;

    return function () {
        return functable.apply(this === GLOBAL ? context : this, arguments);
    };
}

Now you can do this:

var someuser = {
    name: 'George',
    func: function () {
        console.log(this.name);
    }
};

var foo = {
    name: 'foobar'
};

var func = weakBind(someuser.func, foo);
func(); // output foobar

var func2 = weakBind(func, someuser);
func2(); //output George

See the demo: http://jsfiddle.net/R79EG/


The problem with a normal bind is that once you bind an object to the this pointer it can't be overridden. The weak bind checks whether the this pointer is set to the GLOBAL object (in which case the default value is used). Otherwise it uses whatever the new this points to.


BTW in your case it's simply better to do this instead:

var someuser = {
    name: 'George',
    func: function () {
        console.log(this.name);
    }
};

var foo = {
    name: 'foobar'
};

var func = someuser.func.bind(foo);
func(); // output foobar

var func2 = someuser.func.bind(someuser);
func2(); //output George

This is better than weakBind because calling func2 would call func which would in turn call someuser.func. However using bind, calling func2 would directly call someuser.func.

Answer:2

I'm pretty new at JavaScript. I have a variable and two buttons to add or minus 1 from that variable, which works; However I want to make is so that when the variable = 0 the minus button disables, so ...

I'm pretty new at JavaScript. I have a variable and two buttons to add or minus 1 from that variable, which works; However I want to make is so that when the variable = 0 the minus button disables, so ...

I have a progress bar, and I have my js getting the value of it with document.getElementsByTagName(progressbar)[num]. I have this in multiple functions, but when one of the progress bars are removed, ...

I have a progress bar, and I have my js getting the value of it with document.getElementsByTagName(progressbar)[num]. I have this in multiple functions, but when one of the progress bars are removed, ...

I have a question which I still haven't found the answer to. When I use this method to capture a mouse up event: <div onmouseup="/*Script to be executed*/"></div> Is that more ...

I have a question which I still haven't found the answer to. When I use this method to capture a mouse up event: <div onmouseup="/*Script to be executed*/"></div> Is that more ...

  1. javascript addeventlistener onmouseup
  2. window addeventlistener onmouseup
  3. js addeventlistener onmouseup
  4. document addeventlistener onmouseup

What is the purpose of the above line of code. I'm specifically confused by OR-ing the 'function() {}'. Why the empty function? To give a little more context to the line of code, here is the more ...

What is the purpose of the above line of code. I'm specifically confused by OR-ing the 'function() {}'. Why the empty function? To give a little more context to the line of code, here is the more ...