为什么console.log()不显示Object.create的继承属性?

时间:2021-07-02 16:55:58

I am running into a hangup while trying to leverage Object.defineProperty() on a base object. I want to inherit properties from that object, using Object.create(), and then define more properties in the derived object (which may be inherited from there). I should note that I am targetting this at node.js.

我试图在基础对象上利用Object.defineProperty()时遇到挂起。我想使用Object.create()继承该对象的属性,然后在派生对象中定义更多属性(可以从那里继承)。我应该注意到我的目标是node.js.

Here's an example:

这是一个例子:

var Base = {};

Object.defineProperty(Base, 'prop1', {
    enumerable:true,
    get:function(){ return 'prop1 value';}
});

Object.defineProperty(Base, 'prop2', {
    enumerable:true,
    value : 'prop 2 value'
});

Object.defineProperty(Base, 'create', {
    value:function(){
        return Object.create(Base);
    }
});

console.log(Base);

var derived = Base.create();

Object.defineProperty(derived, 'prop3', {
    enumerable:true,
    value:'prop 3 value'
});

console.log(derived);

Which outputs the following:

其中输出如下:

{ prop1: [Getter], prop2: 'prop 2 value' }
{ prop3: 'prop 3 value' }

I thought that console.log() would enumerate the inherited properties, as well as the property prop3 that I defined on the derived object. It would seem that it does not look up the prototype hierarchy for properties defined in this way. Is that correct?

我认为console.log()会枚举继承的属性,以及我在派生对象上定义的属性prop3。看起来它不会查找以这种方式定义的属性的原型层次结构。那是对的吗?

I looked at overriding the toString() method for my object, but it seems that console.log() does not call that.

我查看了覆盖我的对象的toString()方法,但似乎console.log()没有调用它。

  1. How can I get all properties logged without having to enumerate through them?
  2. 如何记录所有属性而不必枚举它们?

  3. Is this a valid way to implement inheritance?
  4. 这是实现继承的有效方法吗?

EDIT:

  1. Is there another function in node.js' libraries that would do the job and log the inherited properties?
  2. node.js的库中是否有另一个函数可以完成这项工作并记录继承的属性?

2 个解决方案

#1


1  

Firebug does log the inherited properties:

Firebug会记录继承的属性:

为什么console.log()不显示Object.create的继承属性?

while Chrome gives you a tree-view which includes the inherited properties:

Chrome会为您提供一个包含继承属性的树视图:

为什么console.log()不显示Object.create的继承属性?

#2


4  

you can use console.dir() where available

你可以在可用的地方使用console.dir()

console.dir(derived) 

and it'll show the inherited properties of your object on the __proto__ object

它将在__proto__对象上显示对象的继承属性

Edit : doesnt seem to show up on node though

编辑:虽然似乎没有显示在节点上

#1


1  

Firebug does log the inherited properties:

Firebug会记录继承的属性:

为什么console.log()不显示Object.create的继承属性?

while Chrome gives you a tree-view which includes the inherited properties:

Chrome会为您提供一个包含继承属性的树视图:

为什么console.log()不显示Object.create的继承属性?

#2


4  

you can use console.dir() where available

你可以在可用的地方使用console.dir()

console.dir(derived) 

and it'll show the inherited properties of your object on the __proto__ object

它将在__proto__对象上显示对象的继承属性

Edit : doesnt seem to show up on node though

编辑:虽然似乎没有显示在节点上