简体   繁体   English

如何在Node中将功能作为模块添加到现有对象?

[英]How to add a function to an existing object as a module in Node?

I am using the D3 library, which exposes a global object called d3 . 我正在使用D3库,该库公开了一个名为d3的全局对象。

I also have a code file which adds a function to this object, called d3.tip . 我也有一个代码文件,它向该对象添加了一个函数d3.tip However, this file is not in the format required for Node modules. 但是,此文件的格式不是节点模块所需的格式。 I would like to convert it to a Node module. 我想将其转换为Node模块。

If I assign the function to module.exports.tip , obviously it won't attach to the d3 object. 如果我将函数分配给module.exports.tip ,显然它将不会附加到d3对象。 How do I write the Node module correctly for this to work? 如何正确编写Node模块以使其正常工作?

当您需要时可以执行以下操作:

d3.tip = require("file").tip;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM