js: accessing scope of parent class

前端 未结 7 574
生来不讨喜
生来不讨喜 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 14:14

    You set "this" to a variable in the parent function and then use it in the inner function.

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

提交回复
热议问题