如何在Javascript中创建常量文件? [英] How to create a file of constants in Javascript?

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

问题描述

有没有办法在JavaScript中创建常量文件,我可以引用然后使用?
我正在寻找的是这样的:

Constants.js:



var Phones =

{

诺基亚:1,

三星:2

}



然后,在另一个JavaScript文件JS2.js访问这些值:
JS2.js:

alert(Phones.Nokia);



然后,在使用它们的aspx文件,引用它们,如:

< asp:ScriptReference Path =../ js / JS2.js/>

< asp:ScriptReference Path =../ js / Constants.js/>



这样的架构可能吗?我们可以使用哪些数据类型?我只是列举了这个枚举,因为这是我现在使用的,但是它们必须在使用的文件中被声明。

解决方案

p>你做的正确。



确保常数首先声明。



JavaScript以自顶向下的顺序解析alls脚本和脚本文件链接。


Is there a way to create a file of constants in JavaScript, which I can reference and then use? What I am looking for is something like this:
Constants.js:

var Phones =
{
Nokia: 1,
Samsung: 2
}

Then, in another JavaScript file JS2.js access those values: JS2.js:
alert(Phones.Nokia);

And then, in an aspx file that uses them, reference both of them, like:
<asp:ScriptReference Path="../js/JS2.js" />
<asp:ScriptReference Path="../js/Constants.js" />

Is such an architecture possible? What datatypes can we use? I only exemplified enums because this is what i use right now, but they must be declared in the same file as they are used.

解决方案

you're doing it right.

make sure the constants are declared first.

JavaScript parses alls scripts and script file links in top down order.

这篇关于如何在Javascript中创建常量文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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