I'm passing an object of a certain type to an array. When I print the object from an outside reference I can see all the fields. When I print the same object from the array reference, I see almost none.
To Explain, here is the code that illustrates the problem:
var test = new OpenLayers.Layer.Vector();
var array = [test];
console.log("******* Printing Test directly ***************");
printObjectProperties(test);
console.log("****** Printing Test from array **************");
for (obj in array){
printObjectProperties(obj);
}
The printObjectProperties() function:
function printObjectProperties(eventData) {
for (var prop in eventData) {
if (!(typeof (eventData[prop]) == "undefined")) {
console.log(prop + " = " + eventData[prop]);
}
};
}
The complete code (complete html file, you can run and see for youself in the browser console log): (live example)
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
$(document).ready(function(){
var test = new OpenLayers.Layer.Vector();
var array = [test];
console.log("******* Printing Test directly ***************");
printObjectProperties(test);
console.log("****** Printing Test from array **************");
for (obj in array){
printObjectProperties(obj);
}
});
function printObjectProperties(eventData) {
for (var prop in eventData) {
if (!(typeof (eventData[prop]) == "undefined")) {
console.log(prop + " = " + eventData[prop]);
}
};
}
</script>
</html>
console.loginstead of trying to format it yourself? Try this and you'll see all the properties: jsbin.com/evazip/3/edit#javascript,htmlfor...inis used to loop over properties of objects. It is much slower than aforloop. For more reasons to not use it for arrays, have a look at developer.mozilla.org/en/JavaScript/Reference/Statements/…