Featured image of post 【JavaScript 模组】汇出模组、汇出部分函式、汇出函式并重新命名

【JavaScript 模组】汇出模组、汇出部分函式、汇出函式并重新命名

【JavaScript 模组】汇出模组、汇出部分函式、汇出函式并重新命名

Photo by Mohammad Rahmani on Unsplash

汇出模组

// math.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;

module.exports = {add, subtract, multiply, divide}

汇出整个模组

// test.js
const math = require('./math');

console.log(math.add(3,5));

汇出部分函式

// test.js
const {add, subtract} = require('./math');

console.log(add(3,5));
console.log(subtract(3,5));

汇出函式并重新命名

// 将 v4 函式重新命名为 uuid
const { v4 : uuid } = require('uuid');

console.log(uuid());

汇入模组

import {myModule} from 'myModule'
import {Method1, Method2} from 'myModule'

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
主题 StackJimmy 设计