简体   繁体   English

Array.push()在重复的对象中,不正确或无法理解

[英]Array.push() in duplicated object, incorrect or incomprehensible

I found an issue/bad behavior Array.push() . 我发现问题/不良行为Array.push() I'm not sure if I'm doing something wrong or if .push() method is incorrect. 我不确定我做错了什么还是.push()方法不正确。

I will present a small example of what I'm dealing with 我将举一个我正在处理的小例子

var x = [];
function test()
{
  var y = x;
  for(var i = 1; i<10; i++)
  {
    y.push(i);
  }
  alert("x = " + x);
}

alert("x = " + x);

test();

//result:
//1'st alert: x =
//2'rd alert: x = 1,2,3,4,5,6,7,8,9

So my example is incomparable small with the real problem what I had in my project, I did fix it: adding methods parameters (x sent as parameter not shared with global scope) or objects cloning where was the case. 因此,我的示例与我在项目中遇到的实际问题相比是无法比拟的,我已经解决了它:添加方法参数(x作为参数发送而未与全局范围共享)或对象克隆(在这种情况下)。

Questions: : 问题

  1. Why push change x when the push is performed on y initialized with x ? 为什么要推动变革x当上进行推y初始化所用x
  2. This example happens cross browsers, and I wondered if node.js do the same well surprise it does, now the question: I'm using wrong .push() method? 这个示例发生在跨浏览器中,我想知道node.js是否能同样出色地完成它,现在是一个问题:我使用了错误的.push()方法? And what is the correct approach to initialize objects from existing ones. 从现有对象初始化对象的正确方法是什么?

Maybe my question is dumb, but I cannot find documented explanation. 也许我的问题很愚蠢,但我找不到记录在案的解释。

Thank you. 谢谢。

Why push change x when the push is performed on y initialized with x ? 为什么要推动变革x当上进行推y初始化所用x

Because y is not "initialized with x " , it is x . 因为y不是“用x初始化” ,所以它 x In Javascript all variables contain references (with the exception of primitive values, like strings or numbers). 在Javascript中,所有变量都包含引用 (原始值(例如字符串或数字)除外)。

x is a name for an array. x是数组的名称。 Stating y = x; 陈述y = x; merely creates another name for the same array. 只是为同一数组创建另一个名称。

This example happens cross browsers, and I wondered if node.js do the same well surprise it does, 这个示例发生在跨浏览器中,我想知道node.js是否能同样出色地完成它,

Of course. 当然。 It's in the spec that way. 就是这种方式。

now the question: I'm using wrong .push() method? 现在的问题:我使用了错误的.push()方法?

Yes. 是。 (Well, no, you're using it right, you just expected the wrong thing.) (好吧,不,您使用的是正确的,只是您预料到了错误的事情。)

And what is the correct approach to initialize objects from existing ones. 从现有对象初始化对象的正确方法是什么?

If you want to clone an object, there are different methods of going about it, depending on whether you want a shallow or a deep clone. 如果要克隆对象,则有不同的处理方法,具体取决于您是浅克隆还是深克隆。 This information can be looked up easily, I won't provide yet another implementation. 此信息可以轻松查找,我将不再提供其他实现。

Related search: https://stackoverflow.com/search?q=javascript+clone+object 相关搜索: https : //stackoverflow.com/search?q=javascript+clone+object

This is happening because you call the function after doing the second alert. 发生这种情况是因为您在执行第二次警报后调用了该函数。 The empty results is from the alert outside the function and the second one is the alert inside the function. 空结果来自功能外部的警报,第二个是功能内部的警报。 This should work properly: 这应该可以正常工作:

var x = [];
function test()
{
  var y = x;
  for(var i = 1; i<10; i++)
  {
    y.push(i);
  }
  alert("x = " + x);
}

test();

alert("x = " + x);

Jsfiddle 的jsfiddle

var x = [];

an Array object is created, a variable x is created, and pointer to the Array object is put in x . 创建一个Array对象,创建一个变量x ,并将指向Array对象的指针放在x

var y = x;

A variable y is created, and pointer to array is copied from x to y . 创建变量y ,并将指向数组的指针从x复制到y Now both variables point to the same Array object. 现在,两个变量都指向同一个Array对象。

It doesn't matter now what variable you will use to push, the same Array object will be pushed. 现在不要紧要使用什么变量来推送相同的Array对象。

If you need to have different array, you need to copy the array, not the pointer. 如果需要其他数组,则需要复制数组,而不是指针。 That is, create new Array object and push it with the values from other Array object. 也就是说,创建新的Array对象,并将其与其他Array对象中的值一起推送。 (Or use some copy function if available) (或使用一些复制功能(如果有))

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

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