js: accessing scope of parent class

前端 未结 7 645
生来不讨喜
生来不讨喜 2021-01-31 13:45

I have a jquery class within a normal class in javascript. Is it possible to access variables in the scope of the parent class from a callback function in the jquery class?

7条回答
  •  不要未来只要你来
    2021-01-31 13:55

    You can mantain state using closure variables:

    function simpleClass() {
       var _state = { status: "pending", target: jqueryObject; }
    
       this.updateStatus = function() {
          this.target.fadeOut("fast",function () {
             _state.status = "complete"; //this needs to update the parent class 
          });
       }
    }
    
    // Later...
    var classInstance = new simpleClass();
    

提交回复
热议问题