如何调用数组中的所有javascript成员函数? [的node.js]

时间:2015-07-03 09:05:12

标签: node.js object iteration

我想迭代并调用与" os"

相关联的所有成员函数
var os = require('os'); 
var keye = Object.keys(os);
keye.forEach(f => os.f());
  

原型:

{ endianness: [Function: getEndianness],
  hostname: [Function: getHostname],
  loadavg: [Function: getLoadAvg],
  uptime: [Function: getUptime],
  freemem: [Function: getFreeMem],
  totalmem: [Function: getTotalMem],
  cpus: [Function: getCPUs],
  type: [Function: getOSType],
  release: [Function: getOSRelease],
  networkInterfaces: [Function: getInterfaceAddresses],
  arch: [Function],
  platform: [Function],
  tmpdir: [Function],
  tmpDir: [Function],
  getNetworkInterfaces: [Function: deprecated],
  EOL: '\r\n' }

1 个答案:

答案 0 :(得分:0)

你需要这样的东西:

Object.keys(os).forEach(function (key) {
    if (typeof os[key] === 'function') {
        console.log(key, os[key]());
    }
});