如何在另一个 javascript 文件中的 javascript 文件中使用 javascript 常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26650721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:11:45  来源:igfitidea点击:

How to use javascript constant in javascript file from another javascript file

javascript

提问by Nitesh

I have created one javascript file in which I have declared different string constants.

我创建了一个 javascript 文件,在其中声明了不同的字符串常量。

now in another javascript file I want to use those String constants from already created javascript file.

现在在另一个 javascript 文件中,我想使用已经创建的 javascript 文件中的那些字符串常量。

Is there any way to do this.

有没有办法做到这一点。

Thanks in advance.

提前致谢。

采纳答案by Cerbrus

If you declare your constants in file1as global variables:

如果您将常量声明file1为全局变量:

var someConstant = 42;

Then you can just use that variable in your other JS files. Just make sure file1is loaded before you try to use them.

然后您就可以在其他 JS 文件中使用该变量。file1在尝试使用它们之前,请确保已加载。

However, polluting the global scope like this come with it's risks, as those variables are easily changed.

然而,像这样污染全局范围是有风险的,因为这些变量很容易改变。

回答by Wottensprels

Multiple ways.

多种方式。

Concatenate

连接

Use a task runner like gruntto define a task that concatenates your files into a single one. This will not only allow convenient definition of constants but also improve performance.

使用像grunt这样的任务运行器来定义一个将您的文件连接成一个的任务。这不仅可以方便地定义常量,还可以提高性能。

There are other tools to do this, too. See Preprosfor windows and codekitfor Macs.

还有其他工具可以做到这一点。请参阅适用于 Windows 的Prepros适用于 Mac 的codekit

Modularize

模块化

If you are on client side, use a tool like require.jsor browserifyto define commonJS / AMD modules and require them as you need.

如果您在客户端,使用像一个工具require.jsbrowserify定义CommonJS的/ AMD模块,并要求他们为你所需要的。

If you are on server side using node, this works out of the box for commonJS.

如果您在服务器端使用 node,这对于 commonJS 来说是开箱即用的。

Load scripts in correct order, expose them through global objects.

按正确顺序加载脚本,通过全局对象公开它们。

You could load your constant defining script first and do something like this:

您可以先加载常量定义脚本并执行以下操作:

var constants = {
    MY_CONST: 'Foo'
}

Your main, whi script could access this variable. If you omit the varkeyword, the variable becomes globally available. Note however that this should be avoided.

您的主要 whi 脚本可以访问此变量。如果省略var关键字,该变量将变为全局可用。但是请注意,应该避免这种情况。

You could even add a property to the global object (windowon the client side).

您甚至可以向全局对象(window在客户端)添加一个属性。



Personally I like to use commonJS modules when working on projects that allow me to do so. Rob Ashtonhas an opinion on this that I would like to share.

我个人喜欢在处理允许我这样做的项目时使用 commonJS 模块。Rob Ashton对此有一个我想分享的观点。

When I can't use those modules in a convenient way, which would be the case when working with (custom) web-components because of their isolated scopes, I like to add a single script which creates an Object like App. I use this object to expose modules, services & constants which can then be required by any component in a neat way:

当我不能以方便的方式使用这些模块时,在使用(自定义)Web 组件时就是这种情况,因为它们的作用域是独立的,我喜欢添加一个脚本来创建一个像App. 我使用这个对象来公开模块、服务和常量,然后任何组件都可以以一种简洁的方式需要它们:

App.myService.doSomething()

App.myService.doSomething()

回答by Shadi

Just use it, and make sure to refer to the javascript file which has all the constants at the first beginning.

只需使用它,并确保在第一个开头引用具有所有常量的 javascript 文件。