Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6:如何在库的多个文件中重用const变量,但不向客户机代码公开它?_Javascript_Ecmascript 6_Es6 Modules - Fatal编程技术网

Javascript ES6:如何在库的多个文件中重用const变量,但不向客户机代码公开它?

Javascript ES6:如何在库的多个文件中重用const变量,但不向客户机代码公开它?,javascript,ecmascript-6,es6-modules,Javascript,Ecmascript 6,Es6 Modules,我正在编写一个由几个文件组成的库: ./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

我正在编写一个由几个文件组成的库:

./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 = () => {
}

...

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 = () => {
}
import { getThatVariable } from "./core.js"


export const file1Function = () => {
    const neededVariable = getThatVariable();
    console.log(neededVariable);
}
然后
file1.js
file2.js
file3.js
类似):

如何使用ES6模块实现这一点?现在,通过此设置,
CONSTANT\u I\u希望\u重用\u库的其他\u文件中的\u也可用于客户端代码

但我想将其保留为“模块专用”,即仅对我的库代码可用


感谢您的关注。

如果您不希望任何变量在模块(文件)外部可见,则只需声明它并在模块内部分配一个值,但不要导出该变量。在
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 = () => {
}

...

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 = () => {
}
import { getThatVariable } from "./core.js"


export const file1Function = () => {
    const neededVariable = getThatVariable();
    console.log(neededVariable);
}
文件1.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 = () => {
}

...

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 = () => {
}
import { getThatVariable } from "./core.js"


export const file1Function = () => {
    const neededVariable = getThatVariable();
    console.log(neededVariable);
}

不要导出该常量。只需声明它,分配一个值并在某些函数中使用。然后导出该函数。您还可以添加
getter
。谢谢您的评论。你能举个例子吗?没有别的办法了,对吧?客户端代码仍然可以访问
getThatVariable
。他不访问您的变量,他只可以获取它的值。但他无法改变这一点。除非你用
setter
给他这个机会。但这是另一个故事)我只是向您展示了最简单的方法,但您也可以通过其他方式“屏蔽”获取该变量。
但他无法改变这一点即使不使用getter,客户端代码也无法更改
常量\u I\u希望\u重用\u库的其他文件中的\u