简体   繁体   English

Javascript类中全局变量的范围

[英]Scope of global variables in a Javascript class

I have a custom Javascript object, that looks like this: 我有一个自定义Javascript对象,如下所示:

var CustomClass = function(settings) {

this.var_1 = false;
this.var_2 = null;
this.var_3 = 0;

}

CustomClass.prototype.method_1 = function(){

  var reader = new FileReader();
reader.onload = (function(cropWidget) {
      this.var_1 = true; 
    });
}

CustomClass.prototype.method_2 = function(){

console.log(this.var_1); // logs 'false' onto the console  
if(this.var_1)
 { // proceed further and do something
 }
}

The CustomObject is instantiated upon: CustomObject实例化于:

$(document).ready(function{;  
  var customObj = new CustomClass({/*json values*/});
});

And then, another DOM event will call upon method_1 like: 然后,另一个DOM事件将调用method_1,例如:

$('#element1').click(function(){
   customObj.method_1(); // this is where var_1 is being set to true
});

The problem happens, when method_2() is being invoked in the DOM by another element, like this: 由另一个元素在DOM 中调用method_2()时,会发生问题 ,如下所示:

$('#element2').click(function(){
  customObj.method_2();
});

it checks for the value of var_1 which as you would recall has been set to true when customObj invoked method_1 它会检查var_1的值,您记得当customObj调用method_1时已将其设置为true

this.var_1 is false, and not true as it should be. this.var_1为false,而不是应为true。 Does this mean the scope of var_1 was set to true only for the scope of the method_1() and still retains it's older value? 这是否意味着仅针对method_1()的范围将var_1的范围设置为true,并且仍保留其旧值? IMO Javascript is pass by reference, so that variable value should have been set to true in it's original place. IMO Javascript通过引用传递,因此变量值应在其原始位置设置为true。

Can someone explain where I am going wrong and how I may go about setting the value of var_1 such that it retains it's new value in method_2 as well ? 有人可以解释我要去哪里哪里以及如何设置var_1的值,以便它还能在method_2中保留它的新值吗?

this.var_1 is false, and not true as it should be. this.var_1为false,而不是应为true。

That's likely because you do not refer to the same object. 这可能是因为您没有引用同一对象。 Your event handler function(){ var customObj = new CustomClass(…); } 您的事件处理程序function(){ var customObj = new CustomClass(…); } function(){ var customObj = new CustomClass(…); } creates an instance and assigns it to a local variable . function(){ var customObj = new CustomClass(…); }创建一个实例,并将其分配到一个局部变量 It will get garbage-collected once the function is ran. 函数运行后将被垃圾回收。

IMO javascript is pass by reference, so that variable value should have been set to true in it's original place. IMO javascript是通过引用传递的,因此变量值应在其原始位置设置为true。

No, javascript is always pass-by-value. 不,JavaScript始终是按值传递的。 Yet, when you are passing objects, you are actually passing values that reference the object so there will be a lot of variables referencing the same "shared" object. 但是,当您传递对象时,实际上是在传递引用该对象的值,因此会有很多变量引用同一“共享”对象。

The problem is that the scope in which you're setting var_1 to true isn't what you want it to be: 问题在于您将var_1设置为true的范围不是您想要的范围:

CustomClass.prototype.method_1 = function(){

  var reader = new FileReader();
  reader.onload = function(cropWidget) {
    this.var_1 = true;
  };
}

You're setting var_ to true in a callback, and the value of this in the callback is not the same as in method_1 . 你设置var_true的回调,和值this回调是一样的method_1

You could use the self = this idiom to fix this: 您可以使用self = this习语来解决此问题:

CustomClass.prototype.method_1 = function(){
  // "this" here refers to the CustomClass instance,
  // so let's store it in "self" so we can use it in the callback
  var self = this; 

  var reader = new FileReader();

  reader.onload = function(cropWidget) {
    // "this" here will not be the CustomClass instance, 
    // so we refer to the "self" variable from above.
    self.var_1 = true;
  };
}

That should solve your problem, though there is still a potential timing issue: if method_2 is called before the FileReader fires its onload event, var_1 won't yet be set to true . 那应该可以解决您的问题,尽管仍然存在潜在的计时问题:如果在FileReader触发其onload事件之前调用method_2 ,则var_1尚未设置为true

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

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