简体   繁体   English

在回调内部访问函数的局部变量

[英]Access local variable of function inside callback

If I create a callback within a function, can I get that callback to access the local variables within that function? 如果在函数中创建回调,是否可以获取该回调以访问该函数中的局部变量?

Obj.prototype.outerFunc = function() 
{
    var x = 0;
    var callback = this.innerFunc;
    callback();
}

Obj.prototype.innerFunc = function()
{
    x++;
}

x naturally is not within the scope of innerFunc and will produce an error if called by itself. x自然不属于innerFunc的范围,并且如果被自身调用将产生错误。 But if I call it from outerFunc can I extend innerFunc's scope in order to access x ? 但是,如果我把它从outerFunc可我延长innerFunc's以访问范围x

Edit: Should've mentioned that I don't want to pass arguments into the function or make x and instance of Obj. 编辑:应该提到我不想将参数传递给函数或使x和Obj的实例。 I'm more looking to treat innerFunc as though it was declared locally in outerFunc . 我更希望把innerFunc ,就像它在当地宣布outerFunc Similar to what can be done below: 类似于以下内容:

Obj.prototype.outerFunc = function()
{
    var x = 0;
    var callback = function() {
        x++;
    }
    callback(); // works
}

Yes: this is exactly what function parameters are for. 是的:这正是功能参数的用途。 They allow you to pass a value from one scope into another. 它们允许您将值从一个范围传递到另一个范围。

Obj.prototype.outerFunc = function() 
{
    var x = 0;
    var callback = this.innerFunc;
    x = callback(x);
}

Obj.prototype.innerFunc = function(x)
{
    x++;
    return x;
}

Note that the value is sent to the other function, not the variable. 请注意,该值将发送到其他函数,而不是变量。 So you need to return the value and assign it in order to use it. 因此,您需要返回值并分配它才能使用它。

If you're using prototypes, just set an instance property: 如果您使用的是原型,则只需设置一个实例属性:

// constructor
var Obj = function () {}

Obj.prototype.outerFunc = function() 
{
   this.x = 0;
   var callback = this.innerFunc.bind(this);
   callback();
}

Obj.prototype.innerFunc = function()
{
   this.x++;
}

var instance = new Obj()
instance.outerFunc()
console.log(instance.x) // returns 1

Edit: But @lonesomeday's answer is a much better solution as it takes a more functional approach avoiding side effects :) 编辑:但是@lonesomeday的答案是一个更好的解决方案,因为它采用了一种功能更强的方法来避免副作用:)

The preffered way of doing this is to assign x to the scope of the object then all functions can access it, via this.x 首选的方法是将x分配给对象的范围,然后所有函数都可以通过this.x访问它

Obj.prototype.outerFunc = function() 
{
    this.x= 0; // set x to zero
    this.innerFunc();

}

Obj.prototype.innerFunc = function(x)
{
    this.x++;
    return this.x;
}

This is a bit hard to solve without knowing why you don't want to pass a parameter; 不知道为什么不希望传递参数,这很难解决。 if you just want to have a specific function signature, maybe a higher-order function might help? 如果您只想拥有特定的功能签名,也许高阶功能可能会有所帮助?

Obj.prototype.outerFunc = function() 
{
    var x = 0;
    var callback = this.innerFunc(x);
    callback();
}

Obj.prototype.innerFunc = function(x)
{
    return function () { /* use x in here */ };
}

This way you have two functions one inside the other. 这样,您就可以在内部拥有两个功能。 The outer one takes the parameter, and the inner one can access the variable that is passed to the outer one. 外部的一个接受参数,内部的一个可以访问传递给外部的变量。

This of course only gives you half of what you demonstrate in your example: You can access the local variable but not modify it. 当然,这仅能为您提供示例所展示内容的一半:您可以访问局部变量,但不能对其进行修改。

You can never access any function's internal variables from outside the function under any circumstances, in an OOP context or otherwise. 在任何情况下,无论是在OOP上下文中还是在其他情况下,您都无法从函数外部访问任何函数的内部变量。

The only sort-of-exception is that a function A defined inside a function B, and returned from that function B, continues to have access to the variables in function B--the basic notion of closure. 唯一的例外是,在函数B中定义并从该函数B返回的函数A继续可以访问函数B中的变量-基本的闭包概念。

I have no idea why you don't want to use instance variables. 我不知道为什么你不想使用实例变量。 That's what they're for--sharing data across methods. 这就是他们的目的-跨方法共享数据。 I have no idea why you don't want to pass values in or out--that's what parameters and return values are for. 我不知道您为什么不希望传入或传出值,这就是参数和返回值的用途。

can I extend innerFunc 's scope in order to access x ? 我可以扩展innerFunc的范围来访问x吗?

No, you can't, whatever that means. 不,您不能,这意味着什么。 There is no such notion in JS. JS中没有这样的概念。

The closest you can come to what you seem to maybe want to do is to define the variable in the constructor: 您似乎最想做的是在构造函数中定义变量:

function Obj() {
  var x = 0;

  this.outerFunc = function() {
    var callback = this.innerFunc;
    callback();
  };

  this.innerFunc = function() {
    x++;
  }
}

However, this will not work as-is because this.innerFunc is missing its context. 但是,这不能按this.innerFunc工作,因为this.innerFunc缺少上下文。 Therefore, you would need to write 因此,您需要编写

var callback = () => this.innerFunc();

However, it's a mystery why you would want to do this instead of just writing this.innerFunc() . 但是,为什么要执行此操作而不是仅仅编写this.innerFunc()仍然是一个谜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM