本文介绍了对象数组 - 重复的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我偶然发现了一个我无法弄清楚的问题。我发现 ,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); --- /代码--- 有没有人对此为何会有任何启发性的解释, ,解决方案可能是什么? 谢谢 解决方案 不,他们不是。 函数SomeType(bar) { this.foo = bar; } SomeType.prototype = { 构造函数:SomeType, doSomething:function(){} }; 为了证明,用这行代替 var obj = new SomeType(j); //操纵对象 obj.doSomething(); o [j] = obj; } //数组不是这里的预期 你还没有说你期待什么。无论如何: // 0,1 window.alert([o [0] .foo,o [1] .foo]); 错误可能在于此处。 也许你发布了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> 不,他们不是。 如果是这样,它们都是相同的对象,情况并非如此。 对象是正确创建的,我已经确认了。 * snip * 你没有说出你的期望。无论如何: 7个不同对象的数组? 错误可能在于此处。 这只是为了说明它后来用于什么。在此次通话之前,阵列 是错误的。 也许你发布了SomeType()或someOtherObj.setArray()的代码。 简化: 函数SomeType(){ //私人变量 var m_id = 0; SomeType.prototype.setId = function(id){ m_id = id; } SomeType.prototype.getId = function(){ 返回m_id; } } 如此btw设置类中的方法是否正确?我对b $ b进行了测试并创建了对象。这种类型的所有对象是否有可能以某种方式在同一个私人上运行。变量? * snip * 简化: 函数SomeType(){ //私人变量 var m_id = 0; SomeType.prototype.setId = function(id){ m_id = id; } 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 outthat variables in JS don''t have local scope inside brackets in say, aloop, but things still doesn''t add up. Also, the phenomena seemed verymuch alike to a closure-related one, but I''m not returning anyfunctions as far as I can see (I''m fresh when it comes to closurestho). Let me illustrate the very basic task at hand, which is fillingan array with new objects. All seven positions in the array returnedpoint to the same object!---code---var o = new Array();for(var j=0; j<7; j++) {//Create objectvar obj = new SomeType();//Manipulate objectobj.doSomething();o[j] = obj;}//Array not what is expected heresomeOtherObj.setArray(o);---/code---I also tried the code below to no avail. I don''t see how it''s anydifferent.---code---var o = new Array();for(var j=0; j<7; j++) {//Create objecto[j] = new SomeType();//Manipulate objecto[j].doSomething();}//Array not what is expected heresomeOtherObj.setArray(o);---/code---Does anyone have any enlightening explanations for why this occurs,and what a solution might be?Thanks 解决方案No, they don''t.function SomeType(bar){this.foo = bar;}SomeType.prototype = {constructor: SomeType,doSomething: function() {}};For proof, replace with this line withvar obj = new SomeType(j);You have not said what you expect. Anyhow:// 0,1window.alert([o[0].foo, o[1].foo]);The error may lie in here.Maybe if you posted the code of SomeType() or someOtherObj.setArray(). http://www.jibbering.com/faq/faq_not...ml#ps1DontWorkPointedEars--Prototype.js was written by people who don''t know javascript for peoplewho don''t know javascript. People who don''t know javascript are notthe best source of advice on designing systems that use javascript.-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>No, they don''t.If so, they are all identical objects, which is not the case. Theobjects are created correctly, I have confirmed it.*snip*You have not said what you expect. Anyhow:An array of 7 different objects?The error may lie in here.This was just to illustrate what it''s beeing used for later. The arrayis wrong before this call.Maybe if you posted the code of SomeType() or someOtherObj.setArray().Simplified:function SomeType() {//private variablesvar 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? Itested it and creating objects worked. Is it possible that all objectsof this type somehow operate on the same "private" variables?*snip*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? Itested it and creating objects worked. Is it possible that all objectsof this type somehow operate on the same "private" variables?I think I was into something here. It seems the code below works as itwas meant to be :)function SomeType() {//private variablesvar m_id = 0;}SomeType.prototype.setId = function(id) {this.m_id = id;}SomeType.prototype.getId = function() {return this.m_id;} 这篇关于对象数组 - 重复的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 14:20