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 導出