Javascript 如何在 jQuery 中存储全局值(不一定是全局变量)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2866866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:16:10  来源:igfitidea点击:

How to store a global value (not necessarily a global variable) in jQuery?

javascriptjqueryglobal-variables

提问by Kris Krause

Currently I am working on a legacy web page that uses a ton of JavaScript, jQuery, Microsoft client JavaScript, and other libraries. The bottom line - I cannot rewrite the entire page from scratch as the business cannot justify it. So... it is what it is. Anyway, I need to pollute (I really tried not too) the global namespace with a variable. There are the three options I was thinking about -

目前,我正在处理一个使用大量 JavaScript、jQuery、Microsoft 客户端 JavaScript 和其他库的遗留网页。底线 - 我不能从头开始重写整个页面,因为业务无法证明它的合理性。所以……就是这样。无论如何,我需要用一个变量污染(我真的没有尝试过)全局命名空间。我正在考虑三个选项 -

  1. Just store/retrieve it using a normal JavaScript declaration - var x = 0;

  2. Use jQuery to store/retrieve the value in a DOM tag - $("body").data("x", 0);

  3. Use a hidden form field, and set/retrieve the value with jQuery - $("whatever").data("x", 0);

  1. 只需使用普通的 JavaScript 声明存储/检索它 - var x = 0;

  2. 使用 jQuery 在 DOM 标签中存储/检索值 - $("body").data("x", 0);

  3. 使用隐藏的表单字段,并使用 jQuery 设置/检索值 - $("whatever").data("x", 0);

Is there a better way? I looked at the existing pile of code, and I do not believe the variable can be scoped in a function.

有没有更好的办法?我查看了现有的一堆代码,我不相信该变量可以在函数中限定范围。

回答by karim79

You can create a namespace inside the jQuery object, like so:

您可以在 jQuery 对象内创建一个命名空间,如下所示:

$.mynamespace = { 
    myVar : "something", 
    myVar2 : "somethingElse" 
}; 

or:

或者:

$.mynamespace = {};
$.mynamespace.myVar = "something";
$.mynamespace.myVar2 = "somethingElse";

Bear in mind, any plugin method named 'mynamespace' will be overwritten so be sure to use a sensible name.

请记住,任何名为“mynamespace”的插件方法都将被覆盖,因此请务必使用合理的名称。

回答by ovais.tariq

For me the best way to handle this situation is to define an object in the window object:

对我来说,处理这种情况的最好方法是在 window 对象中定义一个对象:

window.my_config =
{
    my_var1 : 1,
    my_var1 : 2,
    my_var1 : 3
};

This would keep your scope neat and clean. And whenever you would access the global using window.my_configanyone looking at the code would know that a global is being accessed.

这将使您的范围保持整洁。每当您使用window.my_config查看代码的任何人访问全局时,都会知道正在访问全局。

回答by Pedro Rolo

You can create a hash in the global scope and use it as a namespace:

您可以在全局范围内创建一个哈希并将其用作命名空间:

MyNamepace={}
MyNamespace.newvar = 'value'
// MyNamespace.newvar => 'value'

回答by Umair Jabbar

Just sharing my practice with you, I would make a global object/var in the required JavaScript file with a sensible prefix, as in if I am working on a page where this object will be a text box then I would name it:

只是与您分享我的做法,我会在所需的 JavaScript 文件中创建一个全局对象/变量,并带有一个合理的前缀,就像我在一个页面上工作一样,这个对象将是一个文本框,然后我会命名它:

g_TxtMyValue = 'value';    // g_ specifies it to be a global variable, it is one
                           // of the many conventions used

If you have more than one global variable, you can also have a namespace such as:

如果你有多个全局变量,你也可以有一个命名空间,例如:

my_txt = {};  // For a real site I would use a prefix relative to the project
              // name instead of "my".

my_txt.testValueOne = 'Value one';
my_txt.testValueOne = 'Value two';

These variables will be available to you throughout the website, after they have been initialized.

这些变量在初始化后将在整个网站中提供给您。

I hope this helps.

我希望这有帮助。

回答by Moriz

Just a short notice:

只是一个简短的通知:

Is the fancybox doing AJAX (meaning: if it loads within an iFrame, you should add "parent" to the close method), like this:

Fancybox 是否在执行 AJAX(意思是:如果它在 iFrame 中加载,您应该在 close 方法中添加“父级”),如下所示:

parent.$.fancybox.close();

回答by Eldad

Use underscore isEmpty().

使用下划线isEmpty()

_.isEmpty('')will return true.

_.isEmpty('')会回来true