• Mon 2018.09.17

    ref MDN bind(): https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Global_Objects­/Function/bind

    Goal:
    1) Retrieve the internal contents of an instantiated class
    2) Modify the internal value and retain through subsequent retrieval attempts

    The MDN example works using the Json object notation form. When I attempt to use a class however, I'm greeted with either function not found or undefined

    class TestBind {
    
      constructor() {
    //    var vX = -1;
        var vX = 23;
      }
     
      getX() { 
        return this.vX; 
      }
    
      setX( obj ) {
        this.vX = obj;
      }
    
    }
    exports = TestBind;
    


    The constructor seems to ignore the initial default assignment - why not -1

    var tb = new TestBind();
    var g = tb.getX;
    print g
    
    Undefined
    


    Then if I use a setter, then it's not clear if the constructor initialized correctly

    var tb = new TestBind();
    tb.setX = 42;
    var g = tb.getX;
    print g
    
    =42
    



    If I use the () notation - so which is correct, without the () or using the () ?
    How can function getX() not be found when it is clearly defined?

    var tb = new TestBind();
    var g = tb.getX();
    print g
    
    Uncaught Error: Function "getX" not found!
    


    Following the example from MDN, and applying to this class

    var tb = new TestBind();
    //tb.setX = 42;
    //var g = tb.getX;
    var g = tb.getX();
    print g
    var bg = g.bind(tb);
    print bg
    
    var bg = g.bind(tb);
    Uncaught Error: Cannot read property 'bind' of undefined
    



    VERSION 1v99


    1 Attachment

About

Avatar for Robin @Robin started