Is there a way for me to loop over a Javascript Object's built-in properties?
for...in gets me close to where I want to go, but "A for...in loop does not iterate over built-in properties."
Is there a way for me to loop over a Javascript Object's built-in properties?
for...in gets me close to where I want to go, but "A for...in loop does not iterate over built-in properties."
This will work with JSON. It hasn't been tested much:
<style>
.tree {
margin-left:5px;
}
</style>
<div id='out'></div>
<script type="text/javascript">
data = {"feep":{"bar":{"baz":"37628","quux":{"a":"179","b":"7"}},"foo":"1025"},"Bleh":"1234"}
$('out').innerHTML = renderJSON(data)
function renderJSON(obj) {
var keys = []
var retValue = ""
for (var key in obj) {
//$('out').innerHTML = $('out').innerHTML +"<br />" + key + ", " + obj[key]
if(typeof obj[key] == 'object') {
retValue += "<div class='tree'>" + key
retValue += renderJSON(obj[key])
retValue += "</div>"
}
else {
retValue += "<div class='tree'>" + key + " = " + obj[key] + "</div>"
}
keys.push(key)
}
return retValue
}
</script>
When you say "built in properties", which set of properties are you exactly talking about ?
From Douglas Crockford's 'JavaScript - The Good Parts' :
The for in statement can loop over all of the property names in an object. The enumeration will include all of the properties—including functions and prototype properties that you might not be interested in—so it is necessary to filter out the values you don't want. The most common filters are the hasOwnProperty method and using typeof to exclude functions:
var name; for (name in another_stooge) { if (typeof another_stooge[name] !== 'function') { document.writeln(name + ': ' + another_stooge[name]); } }
The answer is no. You can't enumerate properties that aren't enumerable. There are however at least two ways around this.
The first is to generate all possible combination of characters to use as test property names (think: a, b, c, ... aa, ab, ac, ad, ... ). Given that the standards community is famous for coming up with really long method names (getElementsByTagNames, propertyIsEnumerable) this method will require some patience. :-)
Another approach is to test for known native properties from some predefined list.
For example: For an array
you would test for all known native properties of Function.prototype
:
prototype caller constructor length name apply call toSource toString valueOf toLocaleString
...and things inherited from Object.prototype
:
__defineGetter__ __defineSetter__ hasOwnProperty isPrototypeOf __lookupGetter__
__lookupSetter__ __noSuchMethod__ propertyIsEnumerable unwatch watch
...and things inherited from Array
:
index input pop push reverse shift sort splice unshift concat join slice indexOf lastIndexOf
filter forEach every map some reduce reduceRight
..and finally, and optionally, every enumerable property of the object you are testing:
for (var property in myArrayObject) myPossibleProperties.push( property );
You'll then be able to test every one of those to see if they are present on the object instance.
This won't reveal unknown non-enumerable members (undocumented, or set by other scripts), but will allow you to list what native properties are available.
I found the information on native Array
properties on Mozilla Developer Center and MSDN.
No you can't list object's built in properties. But you can refer to the implementer's reference. For example, to know all the methods and properties of Math object implemented on firefox, you'll use Firefox's Javascript Math Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math