Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在另一个文件上使用函数,无需 背景:_Javascript_Node.js_Node Modules_Server Side - Fatal编程技术网

Javascript 在另一个文件上使用函数,无需 背景:

Javascript 在另一个文件上使用函数,无需 背景:,javascript,node.js,node-modules,server-side,Javascript,Node.js,Node Modules,Server Side,我有3个文件,parent.js,child1.js,和child2.js let child1 = require("./child1.js") let child2 = require("./child2.js") let key = "*****" child1.start(key) child2.start(); let key = false; module.exports = { action: async()

我有3个文件,
parent.js
child1.js
,和
child2.js

let child1 = require("./child1.js")
let child2 = require("./child2.js")
let key = "*****"

child1.start(key)
child2.start();
let key = false;

module.exports = {
    action: async() => {
        return someApi.get(key);
    },
    start: async(_key) => {
        key = key;
    }
}
module.exports = {
    action: async() => {
        let res = await child1.action()
        ...
    },
    start: async() => {
        // startup actions
    }
}
parent.js

let child1 = require("./child1.js")
let child2 = require("./child2.js")
let key = "*****"

child1.start(key)
child2.start();
let key = false;

module.exports = {
    action: async() => {
        return someApi.get(key);
    },
    start: async(_key) => {
        key = key;
    }
}
module.exports = {
    action: async() => {
        let res = await child1.action()
        ...
    },
    start: async() => {
        // startup actions
    }
}
child1.js

let child1 = require("./child1.js")
let child2 = require("./child2.js")
let key = "*****"

child1.start(key)
child2.start();
let key = false;

module.exports = {
    action: async() => {
        return someApi.get(key);
    },
    start: async(_key) => {
        key = key;
    }
}
module.exports = {
    action: async() => {
        let res = await child1.action()
        ...
    },
    start: async() => {
        // startup actions
    }
}
child2.js

let child1 = require("./child1.js")
let child2 = require("./child2.js")
let key = "*****"

child1.start(key)
child2.start();
let key = false;

module.exports = {
    action: async() => {
        return someApi.get(key);
    },
    start: async(_key) => {
        key = key;
    }
}
module.exports = {
    action: async() => {
        let res = await child1.action()
        ...
    },
    start: async() => {
        // startup actions
    }
}
问题 我需要在
child2
内部运行
child1
中的函数,但我不能使用
require
,因为只能有一个
child1


有人知道这个问题的解决方案吗?谢谢

我想你问错问题了:)
如果您需要child1成为独一无二的,您应该使用单音,并在任何需要的地方使用它。

//child1
let instance

module.export = () => {
  if(!instance) {
     instance = {
    action: async() => {
        return someApi.get(key);
    },
    start: async(_key) => {
        key = key;
    }
   }
   return instance
}
我个人不喜欢单音法,但它很方便

您可以尝试的另一种方法是将child1服务实例注入child2“构造函数”
您只需导出函数而不是对象
parent.js

let child1 = require("./child1.js")()
let child2 = require("./child2.js")(child1)
let key = "*****"

child1.start(key)
child2.start();
child2

module.exports = (child1) => {
    
    const action =  async() => {
        let res = await child1.action()
        ...
    }
    const start =  async() => {
        // startup actions
    }
    return {action, start}
}