I start a hang, trying to use Object.defineProperty() for the base object. I want to inherit the properties of this object using Object.create() , and then define more properties in the derived object (which can be inherited from there). I should note that I am aiming for this on node.js.
Here is 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 console.log () would list the inherited properties, as well as the prop3 property that I defined on the derived object. It would seem that he is not looking for a hierarchy of prototypes for properties defined in this way. It is right?
I looked at overriding the toString() method for my object, but console.log () does not seem to call this.
- How can I get all registered properties without having to list them?
- Is this a valid way to implement inheritance?
EDIT:
- Is there any other function in the node.js' libraries that would do the job and register the inherited properties?
source share