开发者

nodejs - util.inspect clarification on showHidden

In nodejs documentation for the util.inspect function, the documentat开发者_JS百科ion states that "If showHidden is true, then the object's non-enumerable properties will be shown too."

Does non-enumerable properties refer to prototypes only? Or are there other non-enumerable properties I haven't considered?

Link to documentation in question: http://nodejs.org/docs/v0.4.8/api/util.html#util.inspect


Enumerable properties and prototype properties are unrelated. It just happens that most (all?) of the prototype properties on native objects are non-enumerable.

To show that both prototype and instance properties can be both enumerable or non-enumerable:

You can create non-enumerable properties on your own objects with defineProperty():

var obj = {};
Object.defineProperty(obj, 'a', {
    value: 1,
    enumerable: false
});

On the other hand, prototype properties that you add (non-native) are by default enumerable, even if you add them to the prototypes of native objects:

Object.prototype.a = 1;
var obj = {};

// Will log "a"
for (var i in obj) {
    console.log(i);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜