简体   繁体   English

如何检查节点REPL中的默认对象属性

[英]How to inspect the default object properties in the Node REPL

I am using Node REPL. 我正在使用Node REPL。 When we define a constructor function like one here: 当我们在这里定义一个constructor函数时,如下所示:

function Rabbit() { }

It's prototype object ie Rabbit.prototype have the constructor property which can be referenced like this: 它是prototype对象,即Rabbit.prototype具有constructor属性,可以这样引用:

>> 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. constructor属性未在Rabbit.prototype对象中列出,但是执行Rabbit.prototype.constructorRabbit.prototype["constructor"]会适当地提供此信息。

>> Rabbit.prototype
   {}

a) How can I view these default properties like constructor , hasOwnProperty , toString , valueOf etc. ? a)如何查看这些默认属性,例如constructorhasOwnPropertytoStringvalueOf等? In browser while using the console , I get a nice little dropdown for that. 在使用控制台的 浏览器中 ,我得到了一个不错的下拉列表。 I am expecting some kind of dir command for that. 我期待为此的某种dir命令。

b) And why there inherited properties are not shown, when I fire Rabbit.prototype in the console ? b)当我在控制台中启动Rabbit.prototype时,为什么没有显示inherited属性? Is it desired because we want to show only user added stuff ? 是否因为我们只想显示用户添加的内容而期望?

c) And where are they actually defined Object or Function ? c)它们实际在哪里定义ObjectFunction

EDIT :- Seems like these are added up to Object.prototype . 编辑:-似乎这些都添加到Object.prototype Reference : 参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype https://developer.mozilla.org/zh-CN/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 ? 我们是否需要通过在每个对象上应用Object.getOwnPropertyNames来遍历原型链,以列出拥有的继承的对象?

Regards. 问候。

Yes, it is required to move down the prototype chain by applying Object.getOwnPropertyNames() to get list of non-enumerable properties also. 是的,它需要通过应用Object.getOwnPropertyNames()来获得非枚举属性列表,从而使prototype chain向下移动。 Got it from here : 从这里得到它:

Is it possible to get the non-enumerable inherited property names of an object? 是否可以获得对象的不可枚举的继承属性名称?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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