如何访问JavaScript中对象内所有嵌套对象的所有函数?

How to access all functions of all nested objects within an object in JavaScript?

我正在导入 [faker][1] 库并创建一个 method 到 return 所有 [faker][1] 的 methods 像这样:

const faker = require('faker')

const getMethods = obj => Object.getOwnPropertyNames(obj).filter((method) => typeof obj[method] === 'object')

console.log(getMethods(faker))

不过,我才刚上一级objects:

[
  'locales',        'locale',
  'localeFallback', 'definitions',
  'fake',           'unique',
  'mersenne',       'random',
  'helpers',        'name',
  'address',        'animal',
  'company',        'finance',
  'image',          'lorem',
  'hacker',         'internet',
  'database',       'phone',
  'date',           'time',
  'commerce',       'system',
  'git',            'vehicle',
  'music',          'datatype'
]

我想获取属于这些 objects 的所有 methods。 [1]: https://www.npmjs.com/package/faker

感谢@VLAZ 的回答,我能够像这样使用 for ...in 来解决这个问题:

const faker = require('faker');

const getObjects = (obj) => {
  return Object.getOwnPropertyNames(obj).filter((method) => typeof obj[method] === 'object' || typeof obj[method] === 'function');
};

const getMethods = (object) => {
  const availableMethods = []; 
  for (const property in object) {
    availableMethods.push(...getObjects(object[property]));
  };

  return availableMethods;
};

getMethods(faker); // returns all methods in an array