5

假设我有一个对象 Airport ,其成员有 airportCode 和 airportCity ,如下所示:

function Airport(airportCode, airportCity) {
        this.airportCode = airportCode;
        this.airportCity = airportCity;
    };     

如何创建可以添加的对象数组 Airport。在 Java 中,这将像这样工作:

while(st.hasMoreTokens()) {
    Airport a = new Airport();
    a.airportCode = st.nextToken();
    a.airportCity = st.nextToken();
    airports.add(a);
}
4

3 回答 3

11

一个非常简短的答案:

airports.push(new Airport("code","city"));
于 2012-07-18T11:43:56.337 回答
7

试试这个:

function Airport(airportCode, airportCity) {
        this.airportCode = airportCode;
        this.airportCity = airportCity;
};

var dataArray = [];

for(var i=0; i< 10; i++){
    dataArray[i] = new Airport("Code-" + i, "City" + i);
}
于 2012-07-17T21:13:44.913 回答
4

并没有那么不同

var airports = [];
while (st.hasMoreTokens()) {
    var a = new Airport();
    a.airportCode = st.nextToken();
    a.airportCity = st.nextToken();
    airports.push(a);
}
于 2012-07-17T21:24:21.800 回答