Last active
December 29, 2015 14:49
-
-
Save nola/7686963 to your computer and use it in GitHub Desktop.
Enumerating over all the properties in an object.
Find out whats in there.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create the object with properties & values. | |
var myCar = new Object(); | |
myCar.make = "Ford"; | |
myCar.model = "Mustang"; | |
myCar.year = 1969; | |
//then LOOP(enumerate) through all the properties of the object myCar | |
function showProps(obj, objName) { | |
var result = ""; | |
for (var i in obj) { | |
if (obj.hasOwnProperty(i)) { | |
result += objName + "." + i + " = " + obj[i] + "\n"; | |
} | |
} | |
return result; | |
} | |
//then call it | |
showProps(myCar, "myCar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is mainly used within the console to see what properties you have access to within the object.