简体   繁体   English

如何在公共函数中被覆盖的公共函数中访问私有变量

[英]How do I access a private variable in a public function that is overridden in the public function

For example 例如

var MyClass = function(){

  var that = this;

  var my_var = "I want this";

  var another_var = "this one is easy";

  this.aPublicFunc = function(my_var){

    console.log(my_var);   // logs "I don't want this";
    console.log(another_var);  // logs "this one is easy";
    console.log(this.my_var);  // logs undefined which makes sense as the this context is the context of the calling function.
    console.log(that.my_var);  // logs undefined
  };
};

var an_object = new MyClass();
var an_object.aPublicFunc("I don't want this");

Don't override it. 不要覆盖它。 It makes the code less readable and more confusing. 它使代码的可读性和混乱性降低。

Private variables like you have with my_var are only accessible from code in the constructor and from the functions defined inside their scope (like in aPublicFunc() ) when it is called. my_var一样的私有变量只能在构造函数中的代码及其范围内定义的函数(如aPublicFunc() )进行访问。 And, to access them, you have to use a normal javascript reference to them. 并且,要访问它们,您必须使用对它们的常规javascript引用。 When you define an argument to aPublicFunc() with the same name, you hide that outer scope variable and there is NO way to reach it as defined. 当您使用相同的名称定义aPublicFunc()的参数时,您将隐藏该外部作用域变量,并且无法按定义方式访问它。 Those private variables are not members of the object, they are variables captured in a closure. 这些私有变量不是对象的成员,而是在闭包中捕获的变量。 In javascript, the only way to reach variables in a closure is from code in the scope of that closure and you can only reach them if nothing has overridden their name. 在javascript中,在闭包中访问变量的唯一方法是从该闭包范围内的代码开始,并且只有在没有任何内容覆盖其名称的情况下,您才能访问它们。

Your simple solution is to change the name of the argument or the local variable to something slightly different. 您的简单解决方案是将参数名称或局部变量更改为稍有不同的名称。 If you really want them to look similar, then put an underscore in front of one of them like this: 如果您确实希望它们看起来相似,则可以在其中一个前面加上下划线,如下所示:

var MyClass = function(){

  var that = this;
  var _my_var = "I want this";
  var _another_var = "this one is easy";

  this.aPublicFunc = function(my_var){

    console.log(_my_var);   // logs "I want this";
    console.log(_another_var);  // logs "this one is easy";
    console.log(my_var);  // logs "I don't want this"
  };
};

var an_object = new MyClass();
var an_object.aPublicFunc("I don't want this");

or make the argument a bit more obvious like this: 或像下面这样使参数更明显:

var MyClass = function(){

  var that = this;
  var my_var = "I want this";
  var another_var = "this one is easy";

  this.aPublicFunc = function(new_my_var){

    console.log(my_var);   // logs "I want this";
    console.log(another_var);  // logs "this one is easy";
    console.log(new_my_var);  // logs "I don't want this"
  };
};

var an_object = new MyClass();
var an_object.aPublicFunc("I don't want this");

You can see this last one work here: http://jsfiddle.net/jfriend00/Jeaaz/ 您可以在这里看到这最后一个作品: http : //jsfiddle.net/jfriend00/Jeaaz/

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

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