JS – 如何在同一个文件中访问一个module.exports函数
我有一个function如下所示:
module.exports = myFunction() { //do some stuff }
我可以从其他文件使用var myFunction = require(.thepath.js)
但是,我怎样才能从它创build的文件中访问它。
我试过myFunction()
和this.myFunction()
但都没有工作。
谢谢
你可以把它保存到一个variables,然后像这样输出variables:
// Create the function const myFunction = function() { } // Call the function myFunction(); // Export the function module.exports = myFunction
var func = module.exports = myFunction(){ //do some stuff here }
现在你可以通过variables来访问。
如果你不想在同一个文件中调用它,你实际上可以在这里使用一个匿名函数
module.exports = function (){ // do some stuff }
只要你给它一个名字,你可以用它的名字来调用它。
module.exports = function myFunction(){
//做一些事情 }
myFunction的()
对不起,我记错了。 在这里有一个范围问题,可以通过单独定义函数来解决。
function myFunction (){ // do some stuff } module.exports = myFunction myFunction()