for循环内外变量同值
Variable the same value both inside and outside for loop
我有一组对象,这些对象将信息提供给生成“标记”的变量:
var markers = [
//testPOW
{ mesh: ["campDome","POW"],
color: iconRed,
location: {lat: 43.30985465943113, lng: 11.898409657895801},
visible: true,
id: "TestPOW",
category: "POW"
},
//testCamps
{ mesh: ["campDome","Camps"],
color: iconBlue,
location: {lat: 27.051081239338288, lng: 21.074973750899662},
visible: true,
id: "TestCamp",
category: "Camps"
},
//testInternedCivilians
{ mesh: ["campDome","InternedCivilians"],
color: iconYellow,
location: {lat: 47.866084023826794, lng: 2.61794289751219},
visible: true,
id: "TestInternedCivilians",
category: "InternedCivilians"
},
]
以下代码生成标记:
//生成标记
for ( var i=0; i < markers.length; i++ ) {
var marker = this.addMarker( {
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
id: markers[i].id,
category: markers[i].category
} );
};
虽然不在生成标记的 for 循环内,但变量 marker
是这样的:
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
这只是最后一个要生成的
在循环内部是这样的:
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
如果在 for 循环外调用,我怎样才能marker
包含所有生成的对象?
创建数组有帮助吗?
let marker = [];
for (var i=0; i < markers.length; i++) {
marker.push(this.addMarker( {
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
id: markers[i].id,
category: markers[i].category
}));
};
或者,可能是一个对象?
let marker = {};
for (var i=0; i < markers.length; i++) {
marker[markers[i].id] = this.addMarker({
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
category: markers[i].category
});
};
我有一组对象,这些对象将信息提供给生成“标记”的变量:
var markers = [
//testPOW
{ mesh: ["campDome","POW"],
color: iconRed,
location: {lat: 43.30985465943113, lng: 11.898409657895801},
visible: true,
id: "TestPOW",
category: "POW"
},
//testCamps
{ mesh: ["campDome","Camps"],
color: iconBlue,
location: {lat: 27.051081239338288, lng: 21.074973750899662},
visible: true,
id: "TestCamp",
category: "Camps"
},
//testInternedCivilians
{ mesh: ["campDome","InternedCivilians"],
color: iconYellow,
location: {lat: 47.866084023826794, lng: 2.61794289751219},
visible: true,
id: "TestInternedCivilians",
category: "InternedCivilians"
},
]
以下代码生成标记: //生成标记
for ( var i=0; i < markers.length; i++ ) {
var marker = this.addMarker( {
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
id: markers[i].id,
category: markers[i].category
} );
};
虽然不在生成标记的 for 循环内,但变量 marker
是这样的:
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
这只是最后一个要生成的
在循环内部是这样的:
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
如果在 for 循环外调用,我怎样才能marker
包含所有生成的对象?
创建数组有帮助吗?
let marker = [];
for (var i=0; i < markers.length; i++) {
marker.push(this.addMarker( {
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
id: markers[i].id,
category: markers[i].category
}));
};
或者,可能是一个对象?
let marker = {};
for (var i=0; i < markers.length; i++) {
marker[markers[i].id] = this.addMarker({
mesh : markers[i].mesh,
color: markers[i].color,
color2: 'white',
location : markers[i].location,
scale: 0.4,
offset: 0,
visible: markers[i].visible,
category: markers[i].category
});
};