javascript - Why does console.log() not show inherited properties from Object.create? - Stack Overflow

admin2025-04-22  0

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.

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?

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

  1. How can I get all properties logged without having to enumerate through them?
  2. Is this a valid way to implement inheritance?

EDIT:

  1. Is there another function in node.js' libraries that would do the job and log the inherited properties?

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.

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?

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

  1. How can I get all properties logged without having to enumerate through them?
  2. Is this a valid way to implement inheritance?

EDIT:

  1. Is there another function in node.js' libraries that would do the job and log the inherited properties?
Share Improve this question edited Nov 9, 2012 at 7:26 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Nov 9, 2012 at 1:33 dwernerdwerner 6,5924 gold badges32 silver badges44 bronze badges 3
  • 1 For the lazy: jsfiddle/aDrjA/1 – Šime Vidas Commented Nov 9, 2012 at 1:37
  • 1 For starters, console's implementation is browser-specific. You really can't rely on it to behave the same way, browser to browser, as each vendor is doing something totally different with the non-standard. When you console.log(object); in Chrome dev-tools, you get an expandable node-tree, which has all of the owned methods and properties, and also has the proto chain, which has the full inheritance stack... Expecting this in every browser is not going to happen -- programs in some browsers (WP7 IE9) will crash if they even see window.console, as they don't even have implementations. – LetterEh Commented Nov 9, 2012 at 1:39
  • 1 "I should note that I am targetting this at node.js." – I Hate Lazy Commented Nov 9, 2012 at 1:43
Add a ment  | 

2 Answers 2

Reset to default 5

you can use console.dir() where available

console.dir(derived) 

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

Edit : doesnt seem to show up on node though

Firebug does log the inherited properties:

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745297998a295085.html

最新回复(0)