javascript:全局变量上的短信?
我正在阅读我的 javascript 文件,我想到了两个问题:
1)我使用了 26 个文本字符串,其中一些在多个函数中重复。 所以我想将它们全部声明为全局变量是否是一个好主意?
2)我的代码95%是javascript,其余的是JQuery。逻辑非常简单。是否值得将我所有的 JS 代码转换为 JQuery?
示例代码:
email = document.getElementById('email').value;
document.myForm.submit();
I was reading through my javascript file, and two questions came to my mind:
1) i have 26 strings of text that i use, and some of them gets repeated in several functions.
So i thought if would be a good idea to declare all them as global vars?
2) 95% of my code is javascript, the rest is JQuery. The logics are very simple. Is it worth converting all my JS code to JQuery?
Sample code:
email = document.getElementById('email').value;
document.myForm.submit();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将常量变量分组在一个位置通常是一个好主意,因此它们仅定义一次,不会重复,并且易于查看和修改。
但是,我更愿意将字典分组为单个对象或模块。这将减少全局命名空间的混乱,表明变量已连接,并允许您更好地迭代和操作它们(如果您愿意的话)。使用数组而不是 x1、x2、...、xn 变量的原因是相同的。
另外,jQuery 只是一个 Javascript 库。鉴于您提供给我们的信息,我真的没有看到尝试转换代码的理由(并且有意外破坏代码的风险)。
It is usually a good idea to group the constant variables in a single spot, so they are defined only once, without duplication, and are easy to look at and modify.
However, I would prefer to group the dictionary into a single object or module. This would reduce clutter on the global namespace, would indicate the variables are connected and would allow you to iterate and manipulate them better (if you wish to do so). It is the same reasoning behind using arrays instead of having x1, x2, ..., xn variables.
Also, jQuery is just a Javascript library. I don't really see a reason to try to convert the code (and risk accidentaly breaking it), given the info you gave us.
1)根据你的场景,你可以声明为全局,我觉得全局变量比到处硬编码更好。
2)Jquery是一个javascript框架(ajax),
所以javascript最好保留,看看你是否有任何复杂的代码,Jquery正在以最小的努力完成,然后你可以在那些地方使用。
1) According to your scenario you can declare as Global, i you feel global variable is better to write than hard-coding everywhere.
2) Jquery is a javascript framework (ajax)
so javascript is better to keep, and see if you have any complex code that Jquery is doing with minimal effort, then you can use in those places.
1) 如果您需要在任何地方访问该字符串,最好只定义一个全局变量来存储所有字符串(它可以是一个数组或对象) )否则考虑将这些函数分组在闭包中(所谓的“立即自执行函数”),然后在创建的范围内声明该数组(或对象)。例如
2) 这取决于:您应该考虑使用仅 jQuery 的构造来编写多少个 LOC,但请记住,使用直接 javascript 某些指令可能会更高效(因为您总是会调用
jQuery
功能)。所以没有一个确定的答案。它可能会有所不同,具体取决于您的代码的用途、复杂程度以及您的结构方式。1) if you need to access that strings everywhere it could be better to define only one global var in which you store all the strings (it could be an
array
or anobject
) otherwise consider grouping those functions in a closure (a so-called "immediately self executed function") and then declaring that array (or object) inside the scope created. e.g.2) It depends: you should take into consideration how many LOC you would write using jQuery-only constructs instead, but remember that some instructions may be more performant using straight javascript (because you would always call the
jQuery
function). So there's not a definitive answer. It could vary depending on what your code does, how much complex it is and how you structured it.