|
|
大力的荒野 · #167: argument of ...· 1 月前 · |
|
|
果断的针织衫 · Error E0310 - I'm a ...· 1 月前 · |
|
|
爱看球的海龟 · use c++ in keil not ...· 1 月前 · |
|
|
光明磊落的米饭 · 控制台(Console) | ...· 2 周前 · |
|
|
坚强的蘑菇 · Nuxt3配置入門 - HackMD· 2 周前 · |
|
|
慷慨大方的茴香 · .NET 6 EFCore WebAPI ...· 1 年前 · |
|
|
有胆有识的炒饭 · FlurlGetJsonAsync报Json ...· 1 年前 · |
|
|
逆袭的创口贴 · c语言删除字符串的子串-掘金· 2 年前 · |
|
|
买醉的海豚 · python绘制地形图-掘金· 2 年前 · |
|
|
仗义的柠檬 · 几种简单的springboot启动后启动一条 ...· 2 年前 · |
我想要实现的是创建一个包含多个函数的模块。
module.js:
module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); },
// This may contain more functions
main.js:
var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);
我遇到的问题是,
firstParam
是一个对象类型,而
secondParam
是一个URL字符串,但当我遇到这个问题时,它总是抱怨类型错误。
在这种情况下,如何声明多个module.exports?
你可以这样做:
module.exports = {
method: function() {},
otherMethod: function() {},
};
或者只是:
exports.method = function() {};
exports.otherMethod = function() {};
然后在调用脚本中:
const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');
您可以编写一个在其他函数之间手动委托的函数:
module.exports = function(arg) {
if(arg instanceof String) {
return doStringThing.apply(this, arguments);
}else{
return doObjectThing.apply(this, arguments);
};
这只是我的参考,因为我想要实现的东西可以通过它来实现。
在
module.js
中
我们可以这样做
module.exports = function ( firstArg, secondArg ) {
function firstFunction ( ) { ... }
function secondFunction ( ) { ... }
function thirdFunction ( ) { ... }
return { firstFunction: firstFunction, secondFunction: secondFunction,
thirdFunction: thirdFunction };
}
在
main.js
中
var name = require('module')(firstArg, secondArg);
module.exports = (function () {
'use strict';
var foo = function () {
return {
public_method: function () {}
var bar = function () {
return {
public_method: function () {}
return {
module_a: foo,
module_b: bar
}());
使用这个
(function()
var exports = module.exports = {};
exports.yourMethod = function (success)
exports.yourMethod2 = function (success)
})();
一种方法是在模块中创建一个新对象,而不是替换它。
例如:
var testone = function () {
console.log('test one');
var testTwo = function () {
console.log('test two');
module.exports.testOne = testOne;
module.exports.testTwo = testTwo;
并调用
var test = require('path_to_file').testOne:
testOne();
要导出多个函数,您可以像这样列出它们:
module.exports = {
function1,
function2,
function3
}
然后在另一个文件中访问它们:
var myFunctions = require("./lib/file.js")
然后,您可以通过调用以下命令来调用每个函数:
myFunctions.function1
myFunctions.function2
myFunctions.function3
如果这些文件是使用ES6导出写入的,则可以这样写:
module.exports = {
...require('./foo'),
...require('./bar'),
};
module.js:
const foo = function(<params>) { ... }
const bar = function(<params>) { ... }
//export modules
module.exports = {
}
main.js:
// import modules
var { foo, bar } = require('module');
// pass your parameters
var f1 = foo(<params>);
var f2 = bar(<params>);
module1.js:
var myFunctions = {
myfunc1:function(){
myfunc2:function(){
myfunc3:function(){
module.exports=myFunctions;
main.js
var myModule = require('./module1');
myModule.myfunc1(); //calling myfunc1 from module
myModule.myfunc2(); //calling myfunc2 from module
myModule.myfunc3(); //calling myfunc3 from module
也可以像这样导出它
const func1 = function (){some code here}
const func2 = function (){some code here}
exports.func1 = func1;
exports.func2 = func2;
或者像下面这样的匿名函数
const func1 = ()=>{some code here}
const func2 = ()=>{some code here}
exports.func1 = func1;
exports.func2 = func2;
如果在模块文件中声明类,而不是在简单对象中声明
文件: UserModule.js
//User Module
class User {
constructor(){
//enter code here
create(params){
//enter code here
class UserInfo {
constructor(){
//enter code here
getUser(userId){
//enter code here
return user;
// export multi
module.exports = [User, UserInfo];
主文件: index.js
// import module like
const { User, UserInfo } = require("./path/to/UserModule");
User.create(params);
UserInfo.getUser(userId);
您也可以使用此方法
module.exports.func1 = ...
module.exports.func2 = ...
或
exports.func1 = ...
exports.func2 = ...
有多种方法可以做到这一点,其中一种方法在下面提到。假设您有这样的.js文件。
let add = function (a, b) {
console.log(a + b);
let sub = function (a, b) {
console.log(a - b);
};
您可以使用以下代码片段导出这些函数,
module.exports.add = add;
module.exports.sub = sub;
您可以通过以下代码片段使用导出的函数,
var add = require('./counter').add;
var sub = require('./counter').sub;
add(1,2);
sub(1,2);
我知道这是一个迟来的回复,但希望这能有所帮助!