问题描述
我创建了一个javascript文件,其中我声明了不同的字符串常量。
I have created one javascript file in which I have declared different string constants.
现在在另一个javascript文件中我想使用那些已经创建的javascript文件中的字符串常量。
now in another javascript file I want to use those String constants from already created javascript file.
有没有办法做到这一点。
Is there any way to do this.
提前致谢。
推荐答案
如果将 file1
中的常量声明为全局变量:
If you declare your constants in file1
as global variables:
var someConstant = 42;
然后你可以在其他JS文件中使用该变量。只需确保在尝试使用 file1
之前加载它们。
Then you can just use that variable in your other JS files. Just make sure file1
is loaded before you try to use them.
然而,,因为这些变量很容易改变。
However, polluting the global scope like this come with it's risks, as those variables are easily changed.
这篇关于如何在另一个javascript文件的javascript文件中使用javascript常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!