Node.js NodeJS,创建一个函数,然后将其导出
如何创建仅在mymodule.js中使用的函数 但也可以从mymodule.js外部访问 当然,我也可以这样做:Node.js NodeJS,创建一个函数,然后将其导出,node.js,Node.js,如何创建仅在mymodule.js中使用的函数 但也可以从mymodule.js外部访问 当然,我也可以这样做: module.exports = { myfunction: function() { return "HELLO"; }, }) 但是,难道没有一种方法可以一次声明一个函数,然后再导出它吗 mymodule.js: var x = function P(inp) { console.log('P'); } module.exports = {
module.exports = {
myfunction: function() {
return "HELLO";
},
})
但是,难道没有一种方法可以一次声明一个函数,然后再导出它吗
mymodule.js:
var x = function P(inp) {
console.log('P');
}
module.exports = {
method: x(),
}
function P(inp) { // you may or may not declare it with "var x ="..both are valid
console.log('P');
}
module.exports = {
method: P // "method" is the name by which you can access the function P from outside
};
module.exports = {
P: P // In this case, "P" is the name by which you can access the function P from outside
};
function P(inp) {
console.log('P');
}
module.exports = P;
other.js:
var mac = require('./mymodule.js');
mac.x(); //<-- does not work
var mac = require('./mymodule.js');
mac.method(); // Call it by the name "method"
var mac=require('./mymodule.js');
mac.x()// 在mymodule.js中:
var x = function P(inp) {
console.log('P');
}
module.exports = {
method: x(),
}
function P(inp) { // you may or may not declare it with "var x ="..both are valid
console.log('P');
}
module.exports = {
method: P // "method" is the name by which you can access the function P from outside
};
module.exports = {
P: P // In this case, "P" is the name by which you can access the function P from outside
};
function P(inp) {
console.log('P');
}
module.exports = P;
在other.js中:
var mac = require('./mymodule.js');
mac.method(); // Call it by the name "method"
如果你愿意,你也可以保留相同的名字。例如,在mymodule.js中:
var x = function P(inp) {
console.log('P');
}
module.exports = {
method: x(),
}
function P(inp) { // you may or may not declare it with "var x ="..both are valid
console.log('P');
}
module.exports = {
method: P // "method" is the name by which you can access the function P from outside
};
module.exports = {
P: P // In this case, "P" is the name by which you can access the function P from outside
};
function P(inp) {
console.log('P');
}
module.exports = P;
您也可以这样导出它:
exports.P = P; // This has the same effect as above example
或:
但是,如果您只想从mymodule.js导出一个函数,那么您可以按照@的建议执行以下操作:
exports.P = P; // This has the same effect as above example
在mymodule.js中:
var x = function P(inp) {
console.log('P');
}
module.exports = {
method: x(),
}
function P(inp) { // you may or may not declare it with "var x ="..both are valid
console.log('P');
}
module.exports = {
method: P // "method" is the name by which you can access the function P from outside
};
module.exports = {
P: P // In this case, "P" is the name by which you can access the function P from outside
};
function P(inp) {
console.log('P');
}
module.exports = P;
在other.js中
var mac = require('./mymodule.js');
mac();
如果只导出JS文件外的一个函数,则无需将所有这些字段添加到模块.exports
对象中。您只需执行module.exports=P代码>。通过设计,node.js方法require
在目标文件中查找module.exports
,并返回它。在本例中,执行如下操作:var mac=require('./mac.js');mac()代码>在我看来更有意义。@LucaArgenziano我同意。我会把它加到我的答案里