node.js - Node.js 中对象如何具有匿名函数

标签 node.js function object anonymous-function

在 Node.js 中,process.hrtime 是这样的对象

hrtime: { [Function: hrtime] bigint: [Function] }, (在 pcoress 对象中)

hrtime 可以由 process.hrtime() 执行,并具有键 bigint 作为由 process.hrtime.bigint() 执行的函数。

我想知道hrtime怎么可能同时是一个函数和对象。

我尝试在对象中包含匿名函数,但失败了。

我怎样才能做到?

process {
  title: 'node',
  version: 'v10.16.3',
...
  hrtime: { [Function: hrtime] bigint: [Function] },
...
}

最佳答案

您可以像为任何对象分配属性一样为函数分配属性。

function x() {
  console.log("You have successfully called x()!");
}

function y() {
  console.log("Hello, this is y()!");
}

x.y = y;

x();
x.y();

打印出来

You have successfully called x()!
Hello, this is y()!

关于node.js - Node.js 中对象如何具有匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57754889/

相关文章:

node.js - 从 Meteor 服务器端路由使用 node-rio

c++ - 使用 C/C++ 检测函数

javascript - 如何将并行列表的对象转换为对象列表?

javascript - Passport.js 在 req.login 上失败

javascript - 独特的验证在 Mongoose 中不起作用

javascript - 有没有办法找出正在尝试访问的对象的哪个键?

javascript - 更改文本的功能不起作用 jQuery

php - WordPress 计划任务删除超过 48 小时的行

javascript - 如何通过动态组合多个其他 json 行中的元素来创建新的 json 行

C++ 返回 MyClass* 与 MyClass