js:访问父类的范围 [英] js: accessing scope of parent class

查看:103
本文介绍了js:访问父类的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中的普通类中有一个jquery类。是否可以从jquery类中的回调函数访问父类范围内的变量?

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?

我的意思的一个简单示例如下所示

A simple example of what I mean is shown below

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

现在在上面的例子中,回调函数试图访问jquery对象的范围。有没有办法访问父类中的状态变量?

Now in the above example, the callback function tries to access the scope of the jquery object. is there any way to access the status variable in the parent class?

推荐答案

您将this设置为父级中的变量函数然后在内部函数中使用它。

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          
            });     
        }; 
    }; 

这篇关于js:访问父类的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆