gpt4 book ai didi

node.js - 定制现有模块

转载 作者:太空宇宙 更新时间:2023-11-03 23:46:48 25 4
gpt4 key购买 nike

我有一组辅助函数,我喜欢将它们与现有的实用程序模块合并在一起。

就像这样:

var customUtil = require('customUtilites');
customUtil.anotherCustomFunction = function() { ... };

exports = customUtil;

这可以通过某种方式实现吗?

最佳答案

你完全可以这样做。

例如

customUtilities.js:

module.exports = {
name: 'Custom'
};

helperA.js

module.exports = function() {
console.log('A');
}

helperB.js:

module.exports = function() {
console.log('B');
}

bundledUtilities.js:

var customUtilities = require('./customUtilities');

customUtilities.helperA = require('./helperA');
customUtilities.helperB = require('./helperB');

module.exports = customUtilities;

ma​​in.js:

var utilities = require('./bundledUtilities');
utilities.helperA();

运行node main.js,您将看到打印的A

关于node.js - 定制现有模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8884245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com