2015-02-24 270 views
0

我遇到了以下問題。 對於一個項目,我得到了多個在一個函數中處理的笛卡爾座標(XYZ)。我嘗試獲取功能返回包含該單獨的XYZ座標被保存在下面的例子中的對象(功能座標(X,Y,Z)的陣列。JS函數返回一個包含對象的數組

var test = Vertices(); 
 

 
var strTemp = "0 => X=" + test[0].X + " Y=" + test[0].Y + " Z=" + test[0].Z + "\n" + 
 
       "1 => X=" + test[1].X + " Y=" + test[1].Y + " Z=" + test[1].Z + "\n" + 
 
       "2 => X=" + test[2].X + " Y=" + test[2].Y + " Z=" + test[2].Z + "\n" + 
 
       "3 => X=" + test[3].X + " Y=" + test[3].Y + " Z=" + test[3].Z + "\n" + 
 
       "4 => X=" + test[4].X + " Y=" + test[4].Y + " Z=" + test[4].Z; 
 
alert(strTemp); 
 

 
// ************************* BELOW ARE THE FUNCTIONS ****************************************** 
 

 
function Coord(X, Y, Z) { 
 
     /// <summary> 
 
     /// Class that represents a cartesian coordinate 
 
     /// </summary> 
 
     /// <param name="X">X value</param> 
 
     /// <param name="Y">Y value</param> 
 
     /// <param name="Z">Z value</param> 
 
    this.X = X; 
 
    this.Y = Y; 
 
    this.Z = Z; 
 
}; 
 

 

 
function Vertices() { 
 
     /// <summary> 
 
    /// Function that collects the locations of all the selected vertices 
 
    /// NOTE: This is a stripped down version of the original function 
 
     /// in order to keep it readable. 
 
     /// </summary> 
 
    /// <returns type="">an array containing cartesian coordinates</returns> 
 

 
    // create an instance of the Coord class for holding the cartesian coordinates 
 
    var location = new Coord("", "", ""); 
 

 
    // initialize the array which will contain the locations 
 
    var Result = []; 
 

 
    // declarations of the index vars. There are two of them because of some logic I removed from this example ;) 
 
    var j = 0; // used as index for the array 
 
    var i = 0; // used as index for the loop 
 

 

 
    // loop thru the selected vertices 
 
    do { 
 

 
     // fill the location object with the cartesian coordinates (currently represented as random values between 0 and 100) 
 
     location.X = randomIntFromInterval(0, 100); 
 
     location.Y = randomIntFromInterval(0, 100); 
 
     location.Z = randomIntFromInterval(0, 100); 
 

 
     // add the location object to the array 
 
     Result.push(location); 
 

 
     // confirm that the values are in the array 
 
     alert(j + "/" + Result.length + " X = " + Result[j].X + " Y = " + Result[j].Y + " Y = " + Result[j].Z); 
 

 
     // increment the indices 
 
     j++; 
 
     i++; 
 

 
    } while (i < 10); 
 

 
    
 
    // return the array to the main program 
 
    return Result; 
 

 
} 
 

 

 

 
function randomIntFromInterval(min, max) { 
 
     /// <summary> 
 
     /// generate random integers between a min and max value 
 
     /// </summary> 
 
     /// <param name="min">lowest possible value</param> 
 
     /// <param name="max">highest possible value</param> 
 
     /// <returns type="">integer</returns> 
 
    //http://stackoverflow.com/questions/4959975/generate-random-value-between-two-numbers-in-javascript 
 
    return Math.floor(Math.random() * (max - min + 1) + min); 
 
}

在這個例子中我有一個名爲「Vertices()」的函數,在本例中它生成一些隨機座標並將它們保存在位置對象中(基於Coord(XYZ))。然後將位置對象添加到數組中。返回到主程序

「頂點」內的警報顯示正確的值但是如果我嘗試使用主程序中的值(請參閱strTemp),所有值都是最後一個添加到數組中的值。

我希望有人能提供一些線索這光...

回答

0

這是因爲

var location = new Coord("", "", ""); 

不是在循環中,因此,您保留對象相同的參考和更新所有索引

0

問題出在Verices函數中。在循環外部創建一個Coords的單個實例,並在內部循環中修改它並推入數組。因此,您的Result數組包含10個指向同一個單一對象的鏈接。

爲了解決這種情況,你應該在每次循環創建Coords新實例:

var location; 
do { 
    location = new Coords("", "", "") 
    // fill the location object with the cartesian coordinates (currently represented as random values between 0 and 100) 
    location.X = randomIntFromInterval(0, 100); 
    location.Y = randomIntFromInterval(0, 100); 
    location.Z = randomIntFromInterval(0, 100); 

    // add the location object to the array 
    Result.push(location); 

    // confirm that the values are in the array 
    alert(j + "/" + Result.length + " X = " + Result[j].X + " Y = " + Result[j].Y + " Y = " + Result[j].Z); 

    // increment the indices 
    j++; 
    i++; 

} while (i < 10); 
+0

謝謝!!!訣竅!正如馬克和克里斯蒂安他們的答案.. – Alban 2015-02-25 08:08:09

0

更換

location = new Coords("", "", "") 
// fill the location object with the cartesian coordinates (currently represented as random values between 0 and 100) 
location.X = randomIntFromInterval(0, 100); 
location.Y = randomIntFromInterval(0, 100); 
location.Z = randomIntFromInterval(0, 100); 

// add the location object to the array 
Result.push(location); 

有:

Result.push(new Coords(randomIntFromInterval(0, 100), randomIntFromInterval(0, 100), randomIntFromInterval(0, 100))); 

這樣,你將用新的隨機值推送新實例。

+0

一個班輪是方便但更復雜的調試,可能不適合初學者:) – Marc 2015-02-24 09:04:14

相關問題