1. Home
  2. Docs
  3. node.js
  4. FAQ
  5. 如何调用其他文件的函数?

如何调用其他文件的函数?

b.js和a.js在相同的目录中, 实现a.js调用b.js文件中的函数. Tip: require 路径是相对路径!

文件: a.js

var objFunc = require('./b')   // 目录名/文件名
objFunc.fff(1,6)
objFunc.func(2,1)

文件: b.js

var func = function (a, b) {
    console.log("调用了object下的objFunc方法。传参:a=", a, ",b=", b);
    return a + b
}
function fff(a, b) {
    console.log("调用了object下的fff方法。传参:a=", a, ",b=", b);
    return a + b
}
module.exports = {
    func,
    fff
}
Was this article helpful to you? Yes No

How can we help?