Node.js Property Traversal Order


In Node.js what is the order when iteration over properties of an object does it iterate over inherited properties before it's own properties or the other way round?

Also is this likely to change in the future aka do I need to write some feature detect for this like below?

var util = require("util");

function testOwnFirst () {
    function A () {
        this.a = 1;

    function B () {
        this.b = 2;
    util.inherits(B, A);

    var objB = new B();
    for (var prop in objB) {
        if(prop === "a") {
            // Traversal starts with inherited properties first
            return false;    
            // Traversal starts with own properties first
        return true;

exports.ownFirst = testOwnFirst();
Problem courtesy of: DeMeNteD


The ECMAScript standard pretty clearly states that an object is an unordered collection of properties (see section 4.3.3 here:…) so you should not in any way rely on the ordering of properties. Node (and the underlying V8 engine) will offer you no guarantees.

The best you can probably do it iterate all of the properties and call hasOwnProperty() with each one to see which are inherited and which are not

Solution courtesy of: ksimons


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.