简体   繁体   中英

How to inspect the default object properties in the Node REPL

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

function Rabbit() { }

It's prototype object ie 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM