如何在另一个javascript文件的javascript文件中使用javascript常量 [英] How to use javascript constant in javascript file from another javascript file

查看:271
本文介绍了如何在另一个javascript文件的javascript文件中使用javascript常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个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常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆