从内部作用域访问外部作用域 [英] Accessing outer scope from inner scope

查看:41
本文介绍了从内部作用域访问外部作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的类型:

I have a type that looks a little something like this:

var x = function(){
    this.y = function(){

    }

    this.z = function(){
        ...
        this.A = function(){
            CALLING POINT
        }
    }
}

从调用点开始,我试图调用函数 this.y.我不需要传递任何参数,但是当我从this.A设置一些东西时,我需要调用this.y.

From calling point, I'm attempting to call function this.y. I don't need to pass any parameters, but when I set some stuff from this.A, I need to call this.y.

这可能吗?我可以将额外的参数传递给函数以使其成为可能.

Is this possible? I'm OK with passing extra parameters to functions to make it possible.

推荐答案

这可能吗?

是的,您可以将 this 引用分配给另一个变量,然后在其上调用函数 y

Yes, you can assign this reference to another variable and then call function y on it

this.z = function() {
    var self = this;
    this.A = function() {
        self.y();
    }
}

这篇关于从内部作用域访问外部作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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