Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript和jQuery的嵌套对象函数中的变量作用域_Javascript_Jquery - Fatal编程技术网

javascript和jQuery的嵌套对象函数中的变量作用域

javascript和jQuery的嵌套对象函数中的变量作用域,javascript,jquery,Javascript,Jquery,我写了两篇文章。一个叫做列表,另一个叫做汽车 function Car() { this.make=""; this.year=""; } function List() { this.cars = []; } 我还有另一个函数,它是一种对象列表的方法,用于读取XML文件的信息,并将其存储在列表对象内的car数组中: List.prototype.readXML = function() { var i = 0; $.get('mydata.xml', function(

我写了两篇文章。一个叫做列表,另一个叫做汽车

function Car()
{
  this.make="";
  this.year="";
}

function List()
{
  this.cars = [];
}
我还有另一个函数,它是一种对象列表的方法,用于读取XML文件的信息,并将其存储在列表对象内的car数组中:

List.prototype.readXML = function()
{
  var i = 0;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){
      //Bug Point!!!
      this.cars.push(new Car()); //Push a Car object into array
      this.cars[i].ID = ($(this).attr("ID"));
     }); 
   });
 }
然而,这是行不通的。每次调试都会告诉我车没有定义。。。我试着用var代替这个来定义汽车。并试图移除这个。cars.push而不是cars.push。但它仍然表示,汽车还没有定义

我假设这个问题中的变量作用域可能有问题。谁能教我怎么做


谢谢大家!

问题在于每个jquery

$.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){
      //Bug Point!!!
      this.cars.push(new Car()); //Push a Car object into array
      this.cars[i].ID = ($(this).attr("ID"));
     }); 
   });
未引用您期望它引用的内容

克服此问题的一种常见方法是将其分配给另一个名为
的变量,即
self

List.prototype.readXML = function()
{
  var i = 0;
  // Create a new variable called 'self' that you can refer to within different function scopes
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){
      self.cars.push(new Car()); //Push a Car object into array
      self.cars[i].ID = ($(this).attr("ID"));
     }); 
   });
 }
这意味着您可以访问需要访问的原始列表对象。此方法利用闭包

希望这有帮助


编辑并解释评论中提出的问题:

List.prototype.readXML = function()
{
  // Create a new variable called 'self' that you can refer to within different function scopes
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(i, domElem){
      var car = new Car();
      car.id = $(domElem).attr("ID");
      self.cars.push(car); //Push a Car object into array
     }); 
   });
 }

问题在于每个jquery都有问题

$.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){
      //Bug Point!!!
      this.cars.push(new Car()); //Push a Car object into array
      this.cars[i].ID = ($(this).attr("ID"));
     }); 
   });
未引用您期望它引用的内容

克服此问题的一种常见方法是将其分配给另一个名为
的变量,即
self

List.prototype.readXML = function()
{
  var i = 0;
  // Create a new variable called 'self' that you can refer to within different function scopes
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){
      self.cars.push(new Car()); //Push a Car object into array
      self.cars[i].ID = ($(this).attr("ID"));
     }); 
   });
 }
这意味着您可以访问需要访问的原始列表对象。此方法利用闭包

希望这有帮助


编辑并解释评论中提出的问题:

List.prototype.readXML = function()
{
  // Create a new variable called 'self' that you can refer to within different function scopes
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(i, domElem){
      var car = new Car();
      car.id = $(domElem).attr("ID");
      self.cars.push(car); //Push a Car object into array
     }); 
   });
 }

您遇到的问题与闭包有关

基本上,这个范围在您的.each语句中发生了变化。.each不再引用列表,它引用XML中的当前“项”

要修复它,请参见


您遇到的问题与闭包有关

基本上,这个范围在您的.each语句中发生了变化。.each不再引用列表,它引用XML中的当前“项”

要修复它,请参见

问题在于,此上下文随Ajax回调而改变。我将在原型上定义addCar方法,并使用该方法添加新车。大概是这样的:

List.prototype.addCar = function(data) {
  this.cars.push(new Car()); //Push a Car object into array
  this.cars[i].ID = ($(data).attr("ID"));
}

List.prototype.readXML = function()
{
  var i = 0;
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){ self.addCar(this); });
   });
 }
问题在于,此上下文随Ajax回调而改变。我将在原型上定义addCar方法,并使用该方法添加新车。大概是这样的:

List.prototype.addCar = function(data) {
  this.cars.push(new Car()); //Push a Car object into array
  this.cars[i].ID = ($(data).attr("ID"));
}

List.prototype.readXML = function()
{
  var i = 0;
  var self = this;
  $.get('mydata.xml', function(xml){
    $(xml).find("item").each(function(){ self.addCar(this); });
   });
 }

谢谢你,福斯特。我想那会有帮助的。这种自我交换确实是克服这个问题的一个聪明的方法。然而,我认为我遇到了另一个使用这行代码的bug:self.cars[I].ID=($(this.attr(“ID”));我在前一行放了一个Car对象,所以我想从这个Car[I].ID语法访问成员(即ID),对吗?调试器刚刚告诉我“UncaughtTypeError:无法设置未定义的属性'ID'”您能帮我吗?谢谢大家!@Arthur0902您可以尝试重写它,就像
函数(i,xml){var car=new car();car.id=$(xml).attr(“id”);self.cars.push(car);}
一样,因为每个函数中的
this
都是i的值,而不是domelement@Arthur0902我已经修改了我原来的问题,包括我建议的来源@AlanFoster,非常感谢!另一个绝妙的解决方案!如果可以的话,我真的想请你喝一杯:)谢谢!谢谢你,福斯特。我想那会有帮助的。这种自我交换确实是克服这个问题的一个聪明的方法。然而,我认为我遇到了另一个使用这行代码的bug:self.cars[I].ID=($(this.attr(“ID”));我在前一行放了一个Car对象,所以我想从这个Car[I].ID语法访问成员(即ID),对吗?调试器刚刚告诉我“UncaughtTypeError:无法设置未定义的属性'ID'”您能帮我吗?谢谢大家!@Arthur0902您可以尝试重写它,就像
函数(i,xml){var car=new car();car.id=$(xml).attr(“id”);self.cars.push(car);}
一样,因为每个函数中的
this
都是i的值,而不是domelement@Arthur0902我已经修改了我原来的问题,包括我建议的来源@AlanFoster,非常感谢!另一个绝妙的解决方案!如果可以的话,我真的想请你喝一杯:)谢谢!感谢您提供了非常有用的资源。这真的很有帮助!感谢您提供了非常有用的资源。这真的很有帮助!