最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

您可以从导入的模块中调用函数,而无需在node.js中调用该模块吗?

IT培训 admin 10浏览 0评论

您可以从导入的模块中调用函数,而无需在node.js中调用该模块吗?

有点愚蠢的问题,只想尝试一下是否可以避免所需模块的名称。

例如:

const MATH = require('./maths.js');
HTTP.createServer(function (request, response) {
    response.write("Add 1 and 5: " + MATH.add(1, 5));
}).listen(8080);

// Add in the maths module
// function add(a, b) {
//     return a + b;
// }

我能做些以下事情吗?

response.write(“ Add 1 and 5:” + add(1,5));

回答如下:

是的,您可以使用ES6解构来购买。怎么样?像这样:

const { add } = require('./maths.js');

HTTP.createServer(function (request, response) {
    response.write("Add 1 and 5: " + add(1, 5));
}).listen(8080);

// Add in the maths module
// function add(a, b) {
//     return a + b;
// }

应该在节点10+(甚至更早的版本)中工作

您可以从导入的模块中调用函数,而无需在node.js中调用该模块吗?

有点愚蠢的问题,只想尝试一下是否可以避免所需模块的名称。

例如:

const MATH = require('./maths.js');
HTTP.createServer(function (request, response) {
    response.write("Add 1 and 5: " + MATH.add(1, 5));
}).listen(8080);

// Add in the maths module
// function add(a, b) {
//     return a + b;
// }

我能做些以下事情吗?

response.write(“ Add 1 and 5:” + add(1,5));

回答如下:

是的,您可以使用ES6解构来购买。怎么样?像这样:

const { add } = require('./maths.js');

HTTP.createServer(function (request, response) {
    response.write("Add 1 and 5: " + add(1, 5));
}).listen(8080);

// Add in the maths module
// function add(a, b) {
//     return a + b;
// }

应该在节点10+(甚至更早的版本)中工作

发布评论

评论列表 (0)

  1. 暂无评论