Object.getOwnPropertyNames behaving differently?

var myObj = {
    a: 2,
    b:3,
    [Symbol.iterator]: function() {
        var o = this;
        var idx = 0;
        var ks = Object.keys( o );
        return {
            next: function() {
                return {
                    value: o[ks[idx++]],
                    done: (idx > ks.length)
                };
            }
        };
    }
};

var obj = {
    a: 2,
    b: 3,
    foo: function() {
        console.log("Foo");
    }
};


Object.getOwnPropertyNames(myObj);
["a", "b"]

Object.getOwnPropertyNames(obj);
["a", "b", "foo"]

I am confused that why “Symbol.iterator” is not showing in the property names of myObj, though this has been added as a property similar to foo is added as a property in obj.?

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

@BenGitter Thanks! I forgot! I was trying to edit but you were faster! :slight_smile:

1 Like