【问题标题】:How object has anonymous function in Node.js对象如何在 Node.js 中具有匿名函数
【发布时间】:2020-01-05 08:52:22
【问题描述】:

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

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

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

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

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

我该怎么做呢?

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

【问题讨论】:

    标签: node.js function object anonymous-function


    【解决方案1】:

    您可以将函数的属性分配给任何对象。

    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()!
    

    【讨论】:

    • 谢谢!不是“对象中的匿名函数”而是“函数的属性”!
    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多