对象数组 - 重复的对象引用 [英] Object-arrays - duplicate object-references

查看:82
本文介绍了对象数组 - 重复的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个我无法弄清楚的问题。我发现

,JS中的变量在括号内没有局部范围,例如,一个

循环,但事情仍然没有加起来。此外,与封闭相关的现象似乎非常相似,但就我所见,我没有退回任何

功能(我'''关于闭合的新鲜感

tho)。让我举例说明手头的基本任务,即用新对象填充数组。数组中的所有七个位置都返回

指向同一个对象!


---代码---


var o = new Array();


for(var j = 0; j< 7; j ++){


//创建对象

var obj = new SomeType();


//操纵对象

obj.doSomething();

o [j] = obj;


}


//数组不是这里的预期


someOtherObj.setArray(o);


--- / code ---

我也试过下面的代码没有无济于事。我不知道它是怎么回事。

---代码---


var o = new Array();


for(var j = 0; j< 7; j ++){


//创建对象

o [j] = new SomeType();


//操纵对象

o [j] .doSomething();


}


//数组不是这里的预期


someOtherObj.setArray(o);


--- /代码---


有没有人对此为何会有任何启发性的解释,

,解决方案可能是什么?


谢谢

解决方案

Are Nybakk写道:


[...]让我举例说明手头的基本任务,即用新对象填充数组
。数组中的所有七个位置都返回

指向同一个对象!



不,他们不是。


---代码---



函数SomeType(bar)

{

this.foo = bar;

}


SomeType.prototype = {

构造函数:SomeType,

doSomething:function(){}

};


var o = new Array();


for(var j = 0; j< 7 ; j ++){


//创建对象

var obj = new SomeType();



为了证明,用这行代替


var obj = new SomeType(j);
< blockquote class =post_quotes>
//操纵对象

obj.doSomething();


o [j] = obj;


}


//数组不是这里的预期



你还没有说你期待什么。无论如何:


// 0,1

window.alert([o [0] .foo,o [1] .foo]);


someOtherObj.setArray(o);



错误可能在于此处。


是否有人对此为何会有任何启发性的解释,

以及解决方案可能是什么?



也许你发布了SomeType()或someOtherObj.setArray()的代码。

http://www.jibbering.com/faq/faq_not...ml#ps1DontWork

PointedEars

-

Prototype.js是由不懂人javascript的人写的

谁不懂javascript。不知道javascript的人不是设计使用javascript的系统的最佳建议来源。

- Richard Cornford,cljs,< f8 ** *****************@news.demon.co.uk>


4月12日上午11点31分, Thomas''PointedEars''Lahn< PointedE ... @ web.de>

写道:


Are Nybakk写道:


[...]让我举例说明手头的基本任务,即用新对象填充数组
。数组中的所有七个位置都返回

指向同一个对象!



不,他们不是。



如果是这样,它们都是相同的对象,情况并非如此。

对象是正确创建的,我已经确认了。


* snip *


//数组不是这里的预期



你没有说出你的期望。无论如何:



7个不同对象的数组?


>

// 0,1

window.alert([o [0] .foo,o [1] .foo]);


someOtherObj.setArray(o);



错误可能在于此处。



这只是为了说明它后来用于什么。在此次通话之前,阵列

是错误的。


>


是否有人对此为何发生任何启发性解释,

以及解决方案可能是什么?



也许你发布了SomeType()或someOtherObj.setArray()的代码。



简化:


函数SomeType(){


//私人变量

var m_id = 0;


SomeType.prototype.setId = function(id){

m_id = id; < br $>
}


SomeType.prototype.getId = function(){

返回m_id;

}


}


如此btw设置类中的方法是否正确?我对b $ b进行了测试并创建了对象。这种类型的所有对象是否有可能以某种方式在同一个私人上运行。变量?


* snip *


* snip *


也许你发布了SomeType()或someOtherObj.setArray()的代码。



简化:


函数SomeType(){


//私人变量

var m_id = 0;


SomeType.prototype.setId = function(id){

m_id = id; < br $>
}


SomeType.prototype.getId = function(){

返回m_id;

}


}


如此btw设置类中的方法是否正确?我对b $ b进行了测试并创建了对象。这种类型的所有对象是否有可能以某种方式在同一个私人上运行。变量?



我想我在这里做了些什么。看起来下面的代码是有用的,因为它是
的意思是:)


函数SomeType(){


//私有变量

var m_id = 0;


}


SomeType.prototype.setId = function (id){

this.m_id = id;

}


SomeType.prototype.getId = function(){

返回this.m_id;

}


Hi, I''ve stumbled into a problem I just can''t figure out. I found out
that variables in JS don''t have local scope inside brackets in say, a
loop, but things still doesn''t add up. Also, the phenomena seemed very
much alike to a closure-related one, but I''m not returning any
functions as far as I can see (I''m fresh when it comes to closures
tho). Let me illustrate the very basic task at hand, which is filling
an array with new objects. All seven positions in the array returned
point to the same object!

---code---

var o = new Array();

for(var j=0; j<7; j++) {

//Create object
var obj = new SomeType();

//Manipulate object
obj.doSomething();
o[j] = obj;

}

//Array not what is expected here

someOtherObj.setArray(o);

---/code---
I also tried the code below to no avail. I don''t see how it''s any
different.
---code---

var o = new Array();

for(var j=0; j<7; j++) {

//Create object
o[j] = new SomeType();

//Manipulate object
o[j].doSomething();

}

//Array not what is expected here

someOtherObj.setArray(o);

---/code---

Does anyone have any enlightening explanations for why this occurs,
and what a solution might be?

Thanks

解决方案

Are Nybakk wrote:

[...] Let me illustrate the very basic task at hand, which is filling
an array with new objects. All seven positions in the array returned
point to the same object!

No, they don''t.

---code---

function SomeType(bar)
{
this.foo = bar;
}

SomeType.prototype = {
constructor: SomeType,
doSomething: function() {}
};

var o = new Array();

for(var j=0; j<7; j++) {

//Create object
var obj = new SomeType();

For proof, replace with this line with

var obj = new SomeType(j);

//Manipulate object
obj.doSomething();
o[j] = obj;

}

//Array not what is expected here

You have not said what you expect. Anyhow:

// 0,1
window.alert([o[0].foo, o[1].foo]);

someOtherObj.setArray(o);

The error may lie in here.

Does anyone have any enlightening explanations for why this occurs,
and what a solution might be?

Maybe if you posted the code of SomeType() or someOtherObj.setArray().

http://www.jibbering.com/faq/faq_not...ml#ps1DontWork
PointedEars
--
Prototype.js was written by people who don''t know javascript for people
who don''t know javascript. People who don''t know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>


On Apr 12, 11:31 am, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:

Are Nybakk wrote:

[...] Let me illustrate the very basic task at hand, which is filling
an array with new objects. All seven positions in the array returned
point to the same object!


No, they don''t.

If so, they are all identical objects, which is not the case. The
objects are created correctly, I have confirmed it.

*snip*

//Array not what is expected here


You have not said what you expect. Anyhow:

An array of 7 different objects?

>
// 0,1
window.alert([o[0].foo, o[1].foo]);

someOtherObj.setArray(o);


The error may lie in here.

This was just to illustrate what it''s beeing used for later. The array
is wrong before this call.

>

Does anyone have any enlightening explanations for why this occurs,
and what a solution might be?


Maybe if you posted the code of SomeType() or someOtherObj.setArray().

Simplified:

function SomeType() {

//private variables
var m_id = 0;

SomeType.prototype.setId = function(id) {
m_id = id;
}

SomeType.prototype.getId = function() {
return m_id;
}

}

Is it correct to set the methods inside the class like this btw? I
tested it and creating objects worked. Is it possible that all objects
of this type somehow operate on the same "private" variables?

*snip*


*snip*

Maybe if you posted the code of SomeType() or someOtherObj.setArray().


Simplified:

function SomeType() {

//private variables
var m_id = 0;

SomeType.prototype.setId = function(id) {
m_id = id;
}

SomeType.prototype.getId = function() {
return m_id;
}

}

Is it correct to set the methods inside the class like this btw? I
tested it and creating objects worked. Is it possible that all objects
of this type somehow operate on the same "private" variables?

I think I was into something here. It seems the code below works as it
was meant to be :)

function SomeType() {

//private variables
var m_id = 0;

}

SomeType.prototype.setId = function(id) {
this.m_id = id;
}

SomeType.prototype.getId = function() {
return this.m_id;
}


这篇关于对象数组 - 重复的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆