Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 创建和访问对象数组时出错_Javascript_Arrays - Fatal编程技术网

Javascript 创建和访问对象数组时出错

Javascript 创建和访问对象数组时出错,javascript,arrays,Javascript,Arrays,鉴于以下声明 function Func1(a, b) { this.a = a; this.b = b; }; 我想为Func1对象数组创建一个占位符。就我所见,我声明了一个项目数组,因此应该已经定义了它。但每次我试图访问数组时,它都会给我“未定义”错误 我还修改了代码,使用declare'this.items=[],而不是'var items=[],这给了我一个不同的错误,如下所示: var Func3 = function () { this.items =

鉴于以下声明

function Func1(a, b) {
    this.a = a;
    this.b = b;
};
我想为Func1对象数组创建一个占位符。就我所见,我声明了一个项目数组,因此应该已经定义了它。但每次我试图访问数组时,它都会给我“未定义”错误

我还修改了代码,使用declare'this.items=[],而不是'var items=[],这给了我一个不同的错误,如下所示:

 var Func3 = function () {
     this.items = [];

     this.addItem = function (a, b) {
         items.push(new Func1(a, b)); // this gives error 'items' is undefined
     }
 };

 var function3 = new Func3();
 function3.addItem(3, 'test'); 
 window.alert(function3.items[0].b); // I want to show 'test' here
我再次修改了addItem函数,仍然出现错误

var Func4 = function () {
    this.items = [];

    this.addItem = function (a, b) {
        items[items.length] = new Func1(a, b); // this gives error 'items' is undefined,  too
    }
};

我就是不知道为什么代码会失败,谷歌搜索也帮不了我多少忙。我可能没有使用正确的关键字进行搜索,但搜索“在javascript中访问数组”并没有给我任何好的结果。我非常感谢你的帮助

当您使用
this.items=[]
,那么您应该使用
this.items
,而不仅仅是
items
。在发布的代码中没有定义为
项的全局或局部变量,这就是为什么会出现这样的错误

this.items.push('...');
当您使用
var items=[]时
在构造函数中,该变量是构造函数的本地变量,因此外部作用域无法访问该变量。您应该定义一个返回值的方法,或者将
定义为实例(对象)属性

这有两种选择:

var Option1 = function () {
    var items = [];

    this.addItem = function (a, b) {
       items.push(new Func1(a, b));
    }

    this.getItems = function() {
       return items;
    }
}
以及:


这看起来像是一个范围问题,在声明项和调用它时使用“This”。检查代码段:

函数Func1(a,b){
这个a=a;
这个.b=b;
};
var Func2=函数(){
此参数为.items=[];
this.addItem=函数(a,b){
此.items.push(新函数1(a,b));
}
}
var function2=new Func2();
功能2.附加项(2,“测试”);

window.alert(function2.items[0].b)如果希望项隐藏在函数外部,则需要添加一个getter函数以访问items变量:

function Func1(a, b) {
    this.a = a;
    this.b = b;
};

var Func2 = function () {
    var items = [];

    this.addItem = function (a, b) {
        items.push(new Func1(a, b));
    }

    this.getItem = function(index) {
      return items[index];
    };
}

var function2 = new Func2();
function2.addItem(2, 'test');
console.log(function2.getItem(0).b);

在这种情况下,避免上下文混淆的方法是缓存自引用

var Func2 = function () {
    var self = this;

    self.items = [] 

    self.addItem = function (a, b) {
        self.items.push(new Func1(a, b));
    }
}

通过这种方式,正确的上下文将被转换为addItem(),并且无论从何处调用,它都应该运行。

代码中有几个问题:

  • var items
    ,只属于构造函数,因此实例不会获得它的副本。因此,在构造函数(
    Func2
    )中使用
    this.items
    ),以便在创建
    Func2
    的实例时,该实例具有该实例变量

  • 我看到你有时使用
    项目
    ,有时使用
    这个.items
    ,总是使用
    这个.items
    。因此,在
    Func3
    Func4
    中,将代码更改为:

    this.addItem = function (a, b) {
       this.items[items.length] = new Func1(a, b);  
    }
    

我希望这能有所帮助。

解释,而不仅仅是交付完成的代码+1@Cerbrus非常感谢。这里还有很大的改进空间:)问题:在选项1中,为什么我们不需要“this.items”?它们是否会导致与全局项(如果有的话)的混淆?@VietNguyen因为我们定义的是一个变量,一个局部变量,而不是一个属性。此设计模式可用于保持值的私有性,以便外部作用域不能直接操作该值。在第二个选项中,该值作为实例的属性公开给外部作用域。是否始终使用“this.items”?是的,即使在括号符号中也是如此。
var Func2 = function () {
    var self = this;

    self.items = [] 

    self.addItem = function (a, b) {
        self.items.push(new Func1(a, b));
    }
}
this.addItem = function (a, b) {
   this.items[items.length] = new Func1(a, b);  
}