我面临一个奇怪的问题,请看下面的代码。var ApplicationParser = (function(){ApplicationParser.prototype.messageFramer;ApplicationParser.prototype.isPreviousFetchingDirection;ApplicationParser.prototype.latestDataArrays = [];ApplicationParser.prototype.dataPointsArray = [];ApplicationParser.prototype.chartObject;ApplicationParser.prototype.titleLabel;ApplicationParser.prototype.chartConfiguration;ApplicationParser.prototype.htmlId;function ApplicationParser(url,dataPointArray, title, htmlId,fetchSize) { this.messageFramer = new MessageFramer(url,fetchSize); this.isPreviousFetchingDirection = true; this.messageFramer.FetchNextData(this,this.isPreviousFetchingDirection); this.dataPointsArray = dataPointArray; this.titleLabel = title; this.htmlId = htmlId; for (var int = 0; int < globalConfiguration.length; int++) { var array_element = globalConfiguration[int]; this.latestDataArrays[array_element] = new Array(); }}return ApplicationParser; }());var app1 = new ApplicationParser('url',dataPointArray,title,"#context", 50000);app1.latestDataArrays = [1,2,3];app1.dataPointsArray = [1,2,3];var app2 = new ApplicationParser('url',dataPointArray,title,"#context", 50000);app2.latestDataArrays = [4,5,6];app2.dataPointsArray = [4,5,6];现在,如果您尝试访问两个对象的“ latestDataArrays”,它将显示[4,5,6];而“ dataPointsArray”保持不同。为什么'latestDataArrays'也不能显示不同的值...好像我用下面的代码替换了上面的代码,那么问题就解决了...var ApplicationParser = (function(){ApplicationParser.prototype.messageFramer;ApplicationParser.prototype.isPreviousFetchingDirection;ApplicationParser.prototype.latestDataArrays = [];ApplicationParser.prototype.dataPointsArray = [];ApplicationParser.prototype.chartObject;ApplicationParser.prototype.titleLabel;ApplicationParser.prototype.chartConfiguration;ApplicationParser.prototype.htmlId;function ApplicationParser(url,dataPointArray, title, htmlId,fetchSize) { this.messageFramer = new MessageFramer(url,fetchSize); this.isPreviousFetchingDirection = true; this.messageFramer.FetchNextData(this,this.isPreviousFetchingDirection); this.dataPointsArray = dataPointArray; this.titleLabel = title; this.htmlId = htmlId; var latArrayTemp = new Object(); for (var int = 0; int < globalConfiguration.length; int++) { var array_element = globalConfiguration[int]; latArrayTemp[array_element] = new Array(); } this.latestDataArrays = latArrayTemp;}return ApplicationParser;}());请说明其背后的原因是什么,它是构造函数内部的for循环创建问题,如果是,那么为什么...? 最佳答案 请说明其背后的原因是什么,构造函数内部的for循环会产生问题吗?不,它与for循环无关。问题的根本原因是:dataPointsArray是app1和app2的实例变量-每个ApplicationParser对象将具有其自己唯一的dataPointsArray实例。但是,latestDataArrays是ApplicationParser.prototype中的一个字段,这意味着所有ApplicationParser对象(app1和app2)将共享latestDataArrays实例(除非他们声明它是latestDataArrays指向其他对象)。这正是第二个代码段的行为。如何解决该问题?由于dataPointsArray和latestDataArrays是实例变量,请从ApplicationParser.prototype中将它们删除。也就是说,删除以下两行:ApplicationParser.prototype.latestDataArrays = [];ApplicationParser.prototype.dataPointsArray = [];一些详细的解释:代码中的var app = new ApplicationParser(...)会发生什么?调用new ApplicationParser(...)时,将执行以下步骤:将创建一个空对象,该对象从ApplicationParser.prototype扩展。我们称它为X。使用X作为函数内的ApplicationParser执行函数this(构造函数)。如果构造函数中没有return语句,则返回X;否则,返回X。将X分配给app。app1和app2会发生什么?对于var app1 = new ApplicationParser(...)语句,我们将其“ X”称为Xapp1。对于var app2 = new ApplicationParser(...)语句,我们将其“ X”称为Xapp2。创建Xapp1之后,在构造函数运行之前,Xapp1.dataPointsArray指向ApplicationParser.prototype.dataPointsArray,Xapp1.latestDataArrays指向ApplicationParser.prototype.latestDataArrays:Xapp1.dataPointsArray === ApplicationParser.prototype.dataPointsArray // trueXapp1.latestDataArrays === ApplicationParser.prototype.latestDataArrays // true创建Xapp1并在构造函数运行后,Xapp1.dataPointsArray指向一个新对象(相应的参数,我们将其称为D1),而Xapp1.latestDataArrays仍指向ApplicationParser.prototype.latestDataArrays,因为它没有重新分配,仅更改this.latestDataArrays[array_element] = ...指向的对象的某些属性。Xapp1.dataPointsArray === ApplicationParser.prototype.dataPointsArray // false, as Xapp1.dataPointsArray is D1 now.Xapp1.latestDataArrays === ApplicationParser.prototype.latestDataArrays // true对于Xapp2,逻辑是相同的,因此:Xapp2.dataPointsArray === ApplicationParser.prototype.dataPointsArray // false, as Xapp2.dataPointsArray is D2 now.Xapp2.latestDataArrays === ApplicationParser.prototype.latestDataArrays // true因此,很明显Xapp1和Xapp2共享this.latestDataArrays的对象,但是它们的latestDataArrays是唯一的。为什么第二个代码段有效?第二个代码段之所以有效,是因为以下语句:this.latestDataArrays = latArrayTemp;该语句重新分配dataPointsArray字段,使Xapp1的latestDataArrays指向一个新对象(我们称其为L1),而Xapp2的latestDataArrays指向另一个对象(L2)。从而:Xapp1.latestDataArrays === ApplicationParser.prototype.latestDataArrays // false, as Xapp1.latestDataArrays is L1 now.Xapp2.latestDataArrays === ApplicationParser.prototype.latestDataArrays // false, as Xapp2.latestDataArrays is L2 now.这使Xapp1和Xapp2的latestDataArrays唯一。关于javascript - 如果我在构造函数中使用for循环,则私有(private)变量不是特定于对象的:JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44796033/ 10-09 20:33