定制现有模块 [英] Customizing existing Module

查看:91
本文介绍了定制现有模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些辅助功能,我想将它们与现有的实用程序模块合并在一起.

I have a collection of helper functions and i like to merge them together with already existing utility modules.

有点像这样:

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

exports = customUtil;

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

Can this be achieved somehow?

推荐答案

您完全可以这样做.

例如

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;

main.js:

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

运行node main.js,您会看到A已打印.

run node main.js you will see A printed.

这篇关于定制现有模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆