Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Node.js NodeJS,创建一个函数,然后将其导出_Node.js - Fatal编程技术网

Node.js NodeJS,创建一个函数,然后将其导出

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 = {

如何创建仅在mymodule.js中使用的函数

但也可以从mymodule.js外部访问

当然,我也可以这样做:

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我同意。我会把它加到我的答案里