• The intended use is:

    // class def
    function TestBind(x) {
      this.x = x;
    }
    TestBind.prototype.getX = function() {
      console.log('TestBind.getX(): returning ' + this.x);
      return this.x;
    }
    
    // usage to create an object instance bound method as function
    var tb = new TestBind(42);
    var gX = tb.getX;
    var bgX = gX.bind(tb);
    // var bgX = tb.getX.bind(tb); 
    

    ...use of the bound method can now be used as plain function in all places where only a function can be passed:

    • setInterval()
    • setTimeout()
    • callback

    in a simplified and fast form:

    setInterval(bgX,1500);
    

    in replacement of anonymous function:

    setInterval(function(){
        tb.gX();
    },1500);
    

    Bind is nothing else than the above, just a bit faster, smarter, and leaner (less resources).

    Often, you will see the pattern of a var _this = this; or var _ = this; in a method to refere to the current object and then the use of if in a 'closures'. Becausethis is runtime executed in the context at that very (future) moment, this as on setup is then not available anymore (or different), and with above approach one makes clear that it mans the current instance. For example with the your example above:

    TestBind.prototype.getX = function() {
      var _this = this;
      setTimeout(function(){
        console.log('.getX(): for TestBind with x=' + _this.x + ' was accessed 10 seconds ago');
      },10000);
      return this.x;
    }
    

    There was a time that this was the only way to be done. But a while now, setTimeout() (and setInterval() ?) allow passing of arguments that are resolved at setup of setTimeout() (and setInterval()?):

    TestBind.prototype.getX = function() {
      setTimeout(function(){
        console.log('.getX(value): for TestBind with x=' + value + ' was accessed 10 seconds ago');
      },10000,this.x);
      return this.x;
    }
    

    Imagine a second method

    TestBind.prototype.doY = function() {
      setTimeout(this.gX.bind(this),5000);
      console.log("...just did Y.");
    }
    

    that will do something in a timeout or interval involving the .getX(), then you can do that with a an anonymous function and 'helper' variable _this or with bind... bind... both work. I do not know the runtime differences and ramification for JS in Espruino vs others...

    
    

    var firstTB = new firstTB(1);
    var secondTB = new firstTB(2);
    firstTB.doY();
    secondTB.doY();

    
    Take also a look at ```apply(...)``` and ```call(...)```.  There are more applications to have a 'function' (or piece / block of - instant - code) as object that is passed around and then then executed. The unbound (undefined object context) or bound (to an object) function is passed around as a reference, and for execution use the regular function argument list parentheses, which is understood as: try to invoke ***the given thing*** as a function an passing the the arguments (if there are). You do that all the time with JS programming. The short form of defining functions obfuscates this a bit, but if you use the *long way with a variable* it becomes obvious... and shows the great Lambda that came to be in the 70'... only Java missed it for so long until recently... so much for progress an new things under the SUN.
    
    Here some fun to play Math Jeopardy with:
    
    

    var f = function(a) { return a * a; }; // square
    var g = function(a) { return a * a * a; }; // cubic

    function j = function(f,x) { // accepts function and a value as args

    console.log("J:  fnct(x) with x = " + x + " returns " + f(x) + ". What is fnct(x) do with x?");
    

    }

    setTimeout(function() {
    j( (Math.random()<0.5) ? f : g, (new Date()).getSeconds() % 6) );
    });

    ;-) I added the JS modulo or modulus  % with 10 so the numbers stay within 0..9 (or % 6 for 0..5) for players -like me when it it's late - used to pocket calculator or phone only and not anymore to learn square up to 59 and all the tricks that go with it like: 25 square is 2*(2+1) and appended 25... goes for a the one with 5 at the end... 125 square is 12*13 + "25"... Espruino will tell you in the console in an instant... this is for square... so for cubic I should simplify even more... also for me...  (it's already late). 
    
    
    
    
    
About

Avatar for allObjects @allObjects started