Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在NodeJ中查看所有属性(包括继承属性)?_Node.js - Fatal编程技术网

Node.js 如何在NodeJ中查看所有属性(包括继承属性)?

Node.js 如何在NodeJ中查看所有属性(包括继承属性)?,node.js,Node.js,在使用nodejs时,我喜欢使用console.log查看对象中有哪些数据可用 但是,这不适用于继承的属性: var Person = function () {}; Person.prototype.name = "anonymous"; var p = new Person(); console.log(['p', p]); // [ 'p', {} ] // This doesn't even give me a hint that it's inherited from Person!

在使用nodejs时,我喜欢使用
console.log
查看对象中有哪些数据可用

但是,这不适用于继承的属性:

var Person = function () {};
Person.prototype.name = "anonymous";
var p = new Person();

console.log(['p', p]); // [ 'p', {} ]
// This doesn't even give me a hint that it's inherited from Person!
console.log(['typeof p', typeof p]); // [ 'typeof p', 'object' ]
console.log(['p.name', p.name]); // "anonymous"

给定一个对象,如何查看我可以访问的所有属性

您正在将属性分配给构造函数
Person
。它不与实例共享属性。您需要将属性添加到
人员的原型中:

Person.prototype.name = "anonymous";
要了解您的对象是否继承自
Person
,您可以执行以下操作:

p instanceof Person; // true
通过执行以下操作,可以打印出对象的所有可枚举属性:

for (var key in p) {
    console.log(key);
}
用于获取属于对象的所有属性:

console.log(Object.getOwnPropertyNames(Person))
// [ 'length', 'name', 'arguments', 'caller', 'prototype' ]

console.log(Object.getOwnPropertyNames(Object))
// ['length','name','arguments','caller','prototype','keys','create',  'defineProperty','defineProperties','freeze','getPrototypeOf','setPrototypeOf','getOwnPropertyDescriptor','getOwnPropertyNames','is','isExtensible','isFrozen','isSealed','preventExtensions','seal','getOwnPropertySymbols','deliverChangeRecords','getNotifier','observe','unobserve','assign' ]
您还可以将
Object.getOwnPropertyNames()
与向上遍历原型链相结合:

var getAllProperties = function (object) {

  var properties = []
  do {
    Object.getOwnPropertyNames(object).forEach((prop) => {
      if (!~properties.indexOf(prop)) {
        properties.push(prop)
      }
    })
  } while (object = Object.getPrototypeOf(object))

  return properties
}

如果只是为了调试,可以检查对象:


在最后一个例子中,与
depth
选项一起使用将允许您进一步查看深度嵌套的对象。

谢谢,我已经更正了示例以修复原型<代码>用于。。。在…
中有帮助,是否有函数用于此?我不知道如何使用
instanceof
来回答“此值是一个简单的对象,还是继承自某个对象?”
function Person() {};
Person.prototype.name = "abc";
Person.prototype.smallObj = {
    name: "abc"
};

Person.prototype.deepObj = {
    one: {
        two: {
            three: {
                four: "4"
            }
        }
    }
};

var p = new Person();

console.log(p);
// Person {}

console.log(p.__proto__);
/*
Person {
    name: 'abc',
    smallObj: { name: 'abc' },
    deepObj: { one: { two: [Object] } }
}
*/
var util = require("util");
console.log(util.inspect(p.__proto__, {depth: null}));
/*
Person {
    name: 'abc',
    smallObj: { name: 'abc' },
    deepObj: { one: { two: { three: { four: '4' } } } }
}
*/