ES6:如何在库的多个文件中重用 const 变量而不将其暴露给客户端代码?
ES6: How to reuse a const variable in multiple files of the library BUT WITHOUT exposing it to client code?
我正在编写一个由多个文件组成的库:
./lib:
- core.js
- file1.js
- file2.js
- file3.js
lib/core.js
包含我需要在 file1.js
、file2.js
和 file3.js
.
中重用的公共变量
例如,core.js
:
/**
* I would like to reuse this constant in file1.js, file2.js and file3.js,
* but I don't want client code to be able to access the value of this constant
* when importing components of my library, like "aFunction" below.
*/
export const CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY = 'Some value';
/**
* I would like to expose this function to client code
* as well as to the other files of the library (file1.js, file2.js and file3.js).
*/
export const aFunction = () => {
}
...
然后file1.js
(file2.js
和file3.js
类似):
import { CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY } from './core'
/**
* This function should be available to the client code (as "aFunction" of core.js),
* but CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY should not.
*/
export const file1Function = () => {
// Need to use `CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY` here
console.log(CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY)
}
如何使用 ES6 模块实现此目的?现在,通过此设置,CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY
也可用于客户端代码。
但我想保留它 "module-private",即仅适用于我的库代码。
感谢关注
如果您不希望任何变量在您的模块(文件)外可见,那么只需声明它并在您的模块内分配一个值,但不要导出 variable.In 您的 core.js
:
const CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY = 'Some value';
export const getThatVariable = () => CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY;
export const aFunction = () => {
}
在你的file1.js
中:
import { getThatVariable } from "./core.js"
export const file1Function = () => {
const neededVariable = getThatVariable();
console.log(neededVariable);
}
我正在编写一个由多个文件组成的库:
./lib:
- core.js
- file1.js
- file2.js
- file3.js
lib/core.js
包含我需要在 file1.js
、file2.js
和 file3.js
.
例如,core.js
:
/**
* I would like to reuse this constant in file1.js, file2.js and file3.js,
* but I don't want client code to be able to access the value of this constant
* when importing components of my library, like "aFunction" below.
*/
export const CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY = 'Some value';
/**
* I would like to expose this function to client code
* as well as to the other files of the library (file1.js, file2.js and file3.js).
*/
export const aFunction = () => {
}
...
然后file1.js
(file2.js
和file3.js
类似):
import { CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY } from './core'
/**
* This function should be available to the client code (as "aFunction" of core.js),
* but CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY should not.
*/
export const file1Function = () => {
// Need to use `CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY` here
console.log(CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY)
}
如何使用 ES6 模块实现此目的?现在,通过此设置,CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY
也可用于客户端代码。
但我想保留它 "module-private",即仅适用于我的库代码。
感谢关注
如果您不希望任何变量在您的模块(文件)外可见,那么只需声明它并在您的模块内分配一个值,但不要导出 variable.In 您的 core.js
:
const CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY = 'Some value';
export const getThatVariable = () => CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY;
export const aFunction = () => {
}
在你的file1.js
中:
import { getThatVariable } from "./core.js"
export const file1Function = () => {
const neededVariable = getThatVariable();
console.log(neededVariable);
}