Javascript 阵列原型中继器功能错误行为

Javascript 阵列原型中继器功能错误行为,javascript,Javascript,我有一个简单的乐趣: Array.prototype.arrayRepeat = function(value, times) { for (var x=0;x<times;x++) this[x]=value; } 当我向第3项添加新值时(例如) 我看到10个“errs”项如何具有“hello”值 我不明白为什么。任何帮助都将不胜感激 编辑: 我想要一个数组。我将“hello”添加到errs[3],因为我想要:errs[3]=[“hello”]。后来可能我想要:errs[3]=

我有一个简单的乐趣:

Array.prototype.arrayRepeat = function(value, times) {  
  for (var x=0;x<times;x++) this[x]=value;
}
当我向第3项添加新值时(例如)

我看到10个“errs”项如何具有“hello”值

我不明白为什么。任何帮助都将不胜感激

编辑:


我想要一个数组。我将“hello”添加到
errs[3]
,因为我想要:
errs[3]=[“hello”]
。后来可能我想要:
errs[3]=[“你好”,“再见”]
,所以我会写
errs[3]。推送(“再见”)

,因为ERR中的数组共享相同的引用。 当你这样做的时候

errs.arrayRepeat([],10);

数组是通过引用“复制”的,因此当一个数组被修改时,其他数组都会被修改。

让我们重写一下

a = [];
errs.arrayRepeat(a, 10);
现在,
errs
在每个位置保持一个参考
a

然后执行错误[3].push('hello')
,这与
a.push('hello')


因为
errs
中的每个元素都是
a
您将得到这个结果。

您将向errs中的每个项添加相同的引用,因此当您向该数组添加项时,对该数组的每个引用都会看到该项

对值进行深度复制的一种简单方法:

Array.prototype.arrayRepeat = function(value, times) {  
    for (var x=0;x<times;x++) this[x]=JSON.parse(JSON.stringify(value))
}
Array.prototype.arrayRepeat=函数(值,时间){

对于(var x=0;x您需要修改arrayRepeat函数,使其如下所示:

Array.prototype.arrayRepeat = function(value, times) {  
  for (var x=0; x < times; x++) this[x] = value.slice();
}
Array.prototype.arrayRepeat=函数(值,时间){
对于(var x=0;x
您的方法适用于数字、字符串或布尔值数组

但它只是为所有索引创建一个数组

在重复之前检查对象

Array.repeater= function(val, len){
    var A= [], 
    check=typeof val== 'object' && val.constructor;

    while(len--){
        if(check){
            val= new val.constructor(val);
        }
        A.push(val);
    }
    return A;
}

//test object

var B= Array.repeater(new Date(2015, 3, 14), 10);
B[3].setDate(16);
B.join('\n')

/*  returned value: (String)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Thu Apr 16 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
*/
without the object check, every item would mirror the changed date

如果值参数类型为“object”,则您的逻辑应该是在每次迭代时创建一个新实例。只有当
value
是数组时,您才应该这样做。
err.arrayRepeat(4,10);
在尝试分割数字时会出错。
Array.prototype.arrayRepeat = function(value, times) {  
  for (var x=0; x < times; x++) this[x] = value.slice();
}
Array.repeater= function(val, len){
    var A= [], 
    check=typeof val== 'object' && val.constructor;

    while(len--){
        if(check){
            val= new val.constructor(val);
        }
        A.push(val);
    }
    return A;
}

//test object

var B= Array.repeater(new Date(2015, 3, 14), 10);
B[3].setDate(16);
B.join('\n')

/*  returned value: (String)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Thu Apr 16 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
Tue Apr 14 2015 00: 00: 00 GMT-0400(Eastern Standard Time)
*/
without the object check, every item would mirror the changed date