1

I am using Node REPL. When we define a constructor function like one here:

function Rabbit() { }

It's prototype object i.e Rabbit.prototype have the constructor property which can be referenced like this:

>> Rabbit.prototype.constructor
   [Function: Rabbit]

This constructor property does not get listed in the Rabbit.prototype object but doing Rabbit.prototype.constructor and Rabbit.prototype["constructor"] gives this info appropriately.

>> Rabbit.prototype
   {}

a) How can I view these default properties like constructor, hasOwnProperty, toString,valueOf etc. ? In browser while using the console, I get a nice little dropdown for that. I am expecting some kind of dir command for that.

b) And why there inherited properties are not shown, when I fire Rabbit.prototype in the console ? Is it desired because we want to show only user added stuff ?

c) And where are they actually defined Object or Function ?

EDIT :- Seems like these are added up to Object.prototype . Reference :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

Also,

>> Object.getOwnPropertyNames(Object.prototype)
[ 'constructor',
  'toString',
  'toLocaleString',
  'valueOf',
  'hasOwnProperty',
  'isPrototypeOf',
  'propertyIsEnumerable',
  '__defineGetter__',
  '__lookupGetter__',
  '__defineSetter__',
  '__lookupSetter__' ]

Do we need to recursively traverse the prototype chain by applying Object.getOwnPropertyNames on each object for listing the owned and inherited ones ?

Regards.

darxtrix
  • 2,032
  • 2
  • 23
  • 30

1 Answers1

1

Yes, it is required to move down the prototype chain by applying Object.getOwnPropertyNames() to get list of non-enumerable properties also. Got it from here :

Is it possible to get the non-enumerable inherited property names of an object?

Community
  • 1
  • 1
darxtrix
  • 2,032
  • 2
  • 23
  • 30