最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing outer scope from inner scope - Stack Overflow

programmeradmin1浏览0评论

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

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.

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

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.

Share Improve this question asked Mar 8, 2016 at 21:42 NatNat 9083 gold badges12 silver badges24 bronze badges 3
  • It depends on how you call your methods, specifically A. – dfsq Commented Mar 8, 2016 at 21:46
  • @T.J.Crowder Could you clarify how this isn't a duplicate of How to access the correct this / context inside a callback? They look the same to me: accessing a property on this from an outer function scope. – apsillers Commented Mar 8, 2016 at 21:58
  • @apsillers: I thought the code there was sufficiently different it would be unclear to the OP how those answers addressed this setup. – T.J. Crowder Commented Mar 9, 2016 at 6:28
Add a comment  | 

4 Answers 4

Reset to default 11

Is this possible?

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

Version with bind, basically this adds a new method a to the object.

var X = function () {
    this.y = function () {
        document.write('y<br>');
    }

    this.z = function () {
        document.write('z<br>');
        this.a = function () {
            document.write('a<br>');
            this.y();
        }
    }.bind(this);
};

var x = new X;
//x.a(); // does not exist
x.z();   // z
x.a();   // a y

Working example with saved inner this.

var X = function () {
    var that = this; // <--

    this.y = function () {
        document.write('y<br>');
    }

    this.Z = function () {
        document.write('Z<br>');
        this.a = function () {
            document.write('a<br>');
            that.y();
        }
    }
}

var x = new X,
    z = new x.Z; // Z

z.a(); // a y

Instead of function() you can try modern JavaScript or Typescript ()=>. I also like .bind(this).

You cannot because this.y() is not within the scope that this.A() is in. You can if you set this.y() to a global function y:

var y = function() {};
var x = function() {
    this.y = y;
    this.z = function() {
       ...
       this.A = function() {
           this.y(); // will be successful in executing because this.y is set to the y function.
       };
    }
};
发布评论

评论列表(0)

  1. 暂无评论