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
myModulewhich 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.jsand 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 applicationbut 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.jsfile 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 导出