• I think as usual @allObjects has done an awesome job of answering the question about bind.

    But looking at the code above, there are a few big issues that I thought it might help if I point out in-line:

    class TestBind {
      constructor() {
        var vX = 23;  // This creates a LOCAL VARIABLE - you need this.vX=23;
      }
     
      getX() { 
        return this.vX; 
      }
      setX( obj ) {
        this.vX = obj;
      }
    }
    exports = TestBind; // not really useful in this case as you're using using 'TestBind' directly
    
    var tb = new TestBind();
    var g = tb.getX; // This makes 'g' equal to the function 'getX' itself
    // Check out https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Functions/get for a real example of getters
    print g // this is not how you call a function in JS. It won't print anything at all. You need print(g)
    
    
    var tb = new TestBind();
    tb.setX = 42; // This just overwrites the 'setX' function with the number 42
    // Check out https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Functions/set for a real example of setters
    
    var g = tb.getX(); // this works, there is no 'Function "getX" not found!' error so I can only assume you're not uploading the code that you posted
    

    I'm not sure any of these problems you're having would be fixed by how you use bind though.

    I think what you intended to do was probably:

    class TestBind {
      constructor() {
        this.vX = 23;
      }
      getX() {
        return this.vX; 
      }
      setX( obj ) {
        this.vX = obj;
      }
    }
    
    var tb = new TestBind();
    print(tb.getX()); // prints 23
    var g = tb.getX();
    print(g); // prints 23
    tb.setX(42);
    print(tb.getX()); // prints 42
    
About

Avatar for Gordon @Gordon started