我正在編寫一個客戶端,以使用JavaScript與服務(wù)器API進行通信。我有OOP背景,但是正在嘗試使用現(xiàn)代EcmaScript。所以我從這里開始:customerApi.js:const baseUrl = "http://myapi";export const getCustomers = () => { /* get customer code */ }export const addCustomer = cust => {}export const deleteCustomer = id => {}所有功能都使用baseUrl。現(xiàn)在,我想進行重構(gòu),以便使用customerApi.js的代碼在baseUrl中設(shè)置/傳遞,而我想出的唯一方法是-使其成為一門課:export default class customerApi { constructor(baseUrl) { this._baseUrl baseUrl; }}將其傳遞給每種方法:export const getCustomers = (baseUrl) => { /* get customer code */ }export const addCustomer = (baseUrl,cust) => {}export const deleteCustomer = (baseUrl,id) => {}包裝功能:const moduleFn = baseUrl => ( return { getCustomers: () => { /* get customer code */ } addCustomer: (cust) => {} deleteCustomer: (id) => {} })export default moduleFn;這些僅僅是示例。在模塊上實現(xiàn)“可設(shè)置”變量的最常見模式是什么?
實現(xiàn)可設(shè)置模塊作用域變量的最常見模式
一只萌萌小番薯
2021-03-29 08:27:49