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

时间:2022-08-25 14:03:38

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 client JavaScript和其他库的遗留web页面。底线是——我不能从头重写整个页面,因为业务不能证明它的合理性。所以…就是这样。无论如何,我需要用一个变量污染全局名称空间(我真的试过了)。有三个选项我正在考虑-

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

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

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

    使用jQuery存储/检索DOM标签中的值- $(“body”)。数据(" x ",0);

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

    使用一个隐藏的表单字段,用jQuery - $(“whatever”)设置/检索值。数据(" 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.

有更好的方法吗?我查看了现有的一堆代码,我认为变量不能在函数中限定范围。

6 个解决方案

#1


100  

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”的插件方法都将被覆盖,所以一定要使用一个合理的名称。

#2


38  

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

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

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_config anyone looking at the code would know that a global is being accessed.

这将使您的范围保持整洁。无论何时你访问全局窗口。任何查看代码的人都知道正在访问全局变量。

#3


6  

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

您可以在全局范围内创建散列,并将其用作名称空间:

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

#4


2  

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文件中创建一个全局对象/var,并使用一个合理的前缀,就像我正在处理一个对象将是一个文本框的页面,那么我将它命名为:

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.

我希望这可以帮助。

#5


0  

Just a short notice: Is the fancybox is AJAX (meaning it loads within an iFrame, you should add "parent" to the close method, like this: js parent.$.fancybox.close();

只需注意一点:fancybox是AJAX(意味着它在iFrame中加载,您应该在close方法中添加“parent”,如:js parent.$.fancybox.close();

#6


0  

Use underscore isEmpty().

使用下划线isEmpty()。

_.isEmpty('') will return true.

_.isEmpty()将返回true。

#1


100  

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”的插件方法都将被覆盖,所以一定要使用一个合理的名称。

#2


38  

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

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

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_config anyone looking at the code would know that a global is being accessed.

这将使您的范围保持整洁。无论何时你访问全局窗口。任何查看代码的人都知道正在访问全局变量。

#3


6  

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

您可以在全局范围内创建散列,并将其用作名称空间:

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

#4


2  

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文件中创建一个全局对象/var,并使用一个合理的前缀,就像我正在处理一个对象将是一个文本框的页面,那么我将它命名为:

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.

我希望这可以帮助。

#5


0  

Just a short notice: Is the fancybox is AJAX (meaning it loads within an iFrame, you should add "parent" to the close method, like this: js parent.$.fancybox.close();

只需注意一点:fancybox是AJAX(意味着它在iFrame中加载,您应该在close方法中添加“parent”,如:js parent.$.fancybox.close();

#6


0  

Use underscore isEmpty().

使用下划线isEmpty()。

_.isEmpty('') will return true.

_.isEmpty()将返回true。