Js变量的范围 [英] Scope of Js variable

查看:91
本文介绍了Js变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景。

文件A.vxml


< script src =。" .. / test.js">

var abc = {};

test.js //没有声明abc js文件。


abc [''hello''] = [" Hello"];


如果我编译A.vxml,它无法从js文件中引用abc变量。

我不想在JS文件中声明abc变量。


有什么方法可以让这个东西工作吗?

I have a scenario.

File A.vxml

<script src=.".. /test.js">
var abc={};


test.js // No declaration of abc in this js file.

abc[''hello'']=["Hello"];

If I compile A.vxml, it is not able to refer to the abc variable from the js file.
I do not want to declare the abc variable in the JS file.

Is there any way I can get this thing working?

推荐答案

我不明白这个问题。如果你不想在JS文件中使用abc并且在编译过程中没有使用JS文件,那么只需将abc保留出去?
I don''t understand the problem. If you don''t want abc in the JS file and the JS file isn''t used during compilation anyway, just leave abc out?


是的,我可能没写问题清楚。


JS文件包含变量arrary ..如...

abc [''LINK''] = [" Link" ];

abc [" IMG''] = ["图片被点击];

....

...........

这个数组将是从包中的各个地方打来的电话。


有一个文件A.vxml在我们的项目启动时被加载..就像在..你可以说这是第一个文件,一旦项目被加载到RAM中开始。所以我将JS文件包含在这个文件中(使用< src>标记)以便JS文件也被加载...因为它将被许多其他程序使用。


一切正常工作如果我在JS文件中声明了abc变量。

我在A.vxml中包含了JS文件(使用src标签)并在vxml文件中只写了声明部分。用法在JS文件中,声明在vxml文件中。


现在当程序的某些部分,当调用abc [IMG]无法检索该条目中的信息时(单击图像)。


这有意义吗?我试图解释这个问题..希望它有所帮助。请给我一些建议。
Yes may be I didn''t write problem clearly.

The JS file contains the variable arrary.. as in ..
abc[''LINK'']=["Link"];
abc["IMG'']=[" Image is clicked"];
....
...........
and this array will be called from various places in the package.

There is this file A.vxml which gets loaded once our project is started .. as in .. you can say that this is the first file which gets loaded into RAM once the project starts. So I am including the JS file in this file ( with use of <src> tag) so that JS file gets loaded too ..because it will be used by many other programs.

Everything is working fine If i declare the abc variable in the JS file itself.
I included the JS file in A.vxml (using src tag) and write just the declaration part in the vxml file. the usage is in the JS file and the declaration is in vxml file.

Now when some part of the program, when calls abc[IMG] is unable to retrieve the information in that entry(The image is clicked).

Does this make sense? I tried to explain the problem .. hope it helps. Kindly provide me with some suggestion.


我想我明白了。


好​​吧,如果你(重新)在脚本标签中声明abc .vxml,你有效地覆盖了包含的JS文件中的变量abc(两个变量都具有相同的(=全局)范围)。
I think I got it.

well, if you (re)declare abc in the script tag in .vxml, you''re effectively overwriting the variable abc in the included JS file (both variables have the same (= global) scope).


这篇关于Js变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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