javascript 导出_用JavaScript导入和导出模块

javascript 导出

We have already seen what Modules are in JavaScript, where they are needed and why and when we should use them. In this article, we'll dive further into the JavaScript Modules and use NodeJS to export and import modules for our application. When we work with modules in NodeJS we can imagine them as libraries that we can import and export so that their code can be used in another file inside the same application.

我们已经了解了JavaScript中的模块 ,需要它们的位置以及为什么以及何时使用它们。 在本文中,我们将进一步深入研究JavaScript模块,并使用NodeJS为我们的应用程序导出和导入模块。 当我们在NodeJS中使用模块时,我们可以将它们想象为可以导入和导出的库,以便它们的代码可以在同一应用程序内的另一个文件中使用。

const myModule=require('./myModule');

Let's say you created a module named myModule which contains some JavaScript code inside it and you want it use it inside your root JS file, say app.js. You can import this module using the "require" keyword which will return a reference to the script.

假设您创建了一个名为myModule的模块,其中包含一些JavaScript代码,并且希望它在根JS文件(例如app.js)中使用它。 您可以使用“ require”关键字导入该模块,该关键字将返回对该脚本的引用。

We can export modules using a special JavaScript object called module.exports. This allows other files to read this file by importing or requiring them first. All the functions, variables and objects that your module contains can now be used by any other file once it imports it.

我们可以使用名为module.exports的特殊JavaScript对象导出模块。 这允许其他文件通过首先导入或要求它们来读取此文件。 模块导入的所有文件现在都可以使用该模块包含的所有函数,变量和对象。

Let's look at an example. Make sure you have the latest stable version of Node.js installed. Create two files, let's call our main file index.js and the other script module.js.

让我们来看一个例子。 确保您已安装最新的稳定版本的Node.js。 创建两个文件,我们将其称为主文件index.js和另一个脚本module.js

module.js:

module.js:

let pokemons=['Squirtle',
'Pikachu',
'Bulbasaur',
'Meowth'];
let attributes= {
    HP: 100, HomeTown: 'Pokeland', isEvolved: false
}

module.exports= {
    pokemons,
    attributes
}

Inside our index.js, let's import this module and log something to the console,

在我们的index.js内部,让我们导入该模块并将某些内容记录到控制台,

const pokemodule =  require('./module'); 
console.log(pokemodule.pokemons);

Command line: node index

命令行:节点索引

Output

输出量

[ 'Squirtle', 'Pikachu', 'Bulbasaur', 'Meowth' ]

We imported a module inside our script and use a variable that should not have been accessible in that script otherwise. This is a basic example of how to import and export modules in JavaScript. Let's take it further to build something useful. Imagine we're building a calculator application but we have no idea what plus-minus means. Thankfully we have a fellow dev who's also skilled at math so he writes down some basic arithmetic functions for us inside a module. Here's what he wrote looks like,

我们在脚本中导入了一个模块,并使用了该脚本中不应该访问的变量。 这是如何在JavaScript中导入和导出模块的基本示例。 让我们进一步构建一些有用的东西。 想象一下我们正在构建一个计算器应用程序,但是我们不知道加减是什么意思。 值得庆幸的是,我们有一位同样精通数学的开发人员,因此他在模块内为我们写下了一些基本的算术函数。 这是他写的样子

calc.js:

calc.js:

let add = (a, b) => a + b;
let subtract = (a, b) => a - b;
let multiply = (a, b) => a * b;
let divide = (a, b) => a / b;
let remainder = (a, b) => a % b;
module.exports = {
    add,
    subtract,
    multiply,
    divide,
    remainder
}

Now all we got to do is import it inside our main file and invoke its methods and display those results and we can do this because he exported that module giving us the ability to import it and use the variables, functions defined inside that module,

现在我们要做的就是将其导入到主文件中,并调用其方法并显示这些结果,我们可以这样做,因为他导出了该模块,从而使我们能够导入它并使用该模块中定义的变量,函数,

index.js:

index.js:

const calc =  require('./calc'); 
let a=10, b=5;
let sum=calc.add(a,b);
let diff=calc.subtract(a,b);
let prod=calc.multiply(a,b);
let mod=calc.remainder(a,b);

console.log(`${a} + ${b} = ${sum}`);
console.log(`${a} - ${b} = ${diff}`);
console.log(`${a} X ${b} = ${prod}`);
console.log(`${a} % ${b} = ${mod}`);

Let's run our index.js file using Node Js,

让我们使用Node Js运行index.js文件,

Command line: node index

命令行:节点索引

Output

输出量

10 + 5 = 15
10 - 5 = 5
10 X 5 = 50
10 % 5 = 0

This is a basic example of a use case where we use modules in JavaScript. A large module may contain some code which could be very useful to us and we'd have absolutely no idea about it. However, we can still look up that module and use certain functions inside it which could be very useful to us. Common examples of modules are express, body-parser that we typically use every time we're building a backend for our application that help us with creating a server and reading the response's body.

这是一个用例的基本示例,其中我们在JavaScript中使用模块。 大模块可能包含一些对我们非常有用的代码,我们对此一无所知。 但是,我们仍然可以查找该模块并在其中使用某些功能,这对我们非常有用。 模块的常见示例是快速的正文分析器,我们通常在每次为应用程序构建后端时都会使用它们,以帮助我们创建服务器并读取响应的正文。

翻译自: https://www.includehelp.com/code-snippets/importing-and-exporting-modules-in-javascript.aspx

javascript 导出

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值