在 JavaScript 'window' 对象中存储变量是使用该对象的正确方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12393303/
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
Storing a variable in the JavaScript 'window' object is a proper way to use that object?
提问by Backo
(Maybe) I just solved a my problem (How to update front-end content after that a form is successfully submitted from a dialog window?) by "storing" / "saving" a variable in the JavaScript window
object. However, since I am newbie in JavaScript matters, I have some doubts if storing / saving a variable in the JavaScript window
object is a "common" / "proper" way to use that object. Is it?
(也许)我刚刚通过在 JavaScript对象中“存储”/“保存”一个变量解决了我的问题(如何在从对话框窗口成功提交表单后更新前端内容?)window
。但是,由于我是 JavaScript 方面的新手,所以我怀疑在 JavaScriptwindow
对象中存储/保存变量是否是使用该对象的“常见”/“正确”方式。是吗?
For example, using the following code
例如,使用以下代码
$('.trigger').click(function() {
window.trigger_link = this;
});
is advisable?
是可取的?
回答by Blazemonger
In JavaScript, any global variable is actually a property of the window
object. Using one is equivalent to (and interchangeable with) using the other.
在 JavaScript 中,任何全局变量实际上都是window
对象的一个属性。使用一个等同于(并且可以互换)使用另一个。
Using global variables is certainly "common," so the question is whether or not it's "proper." Generally, global variables are discouraged, because they can be accessed from ANY function and you risk having multiple functions trying to read from and write to the same variables. (This is true with any programming language in any environment, not just JavaScript.)
使用全局变量当然是“常见的”,所以问题是它是否“合适”。通常,不鼓励使用全局变量,因为它们可以从任何函数访问,并且您可能会有多个函数尝试读取和写入相同的变量。(这适用于任何环境中的任何编程语言,而不仅仅是 JavaScript。)
Solve this problem by creating a namespace unique to your application. The easiest approach is to create a global object with a unique name, with your variables as properties of that object:
通过为您的应用程序创建一个唯一的命名空间来解决这个问题。最简单的方法是创建一个具有唯一名称的全局对象,并将变量作为该对象的属性:
window.MyLib = {}; // global Object container; don't use var
MyLib.value = 1;
MyLib.increment = function() { MyLib.value++; }
MyLib.show = function() { alert(MyLib.value); }
MyLib.value=6;
MyLib.increment();
MyLib.show(); // alerts 7
Another approach is to use .data()
to attach variables to a relevant DOM element. This is not practical in all cases, but it's a good way to get variables that can be accessedglobally without leaving them in the global namespace.
另一种方法是使用.data()
将变量附加到相关的 DOM 元素。这并非在所有情况下都实用,但这是获取可以全局访问的变量而不将它们留在全局命名空间中的好方法。
回答by J.D.
What is actually not mentioned here, and is probably the biggest deal breaker on why not to usewindow
as global scope carrieris that it can be frozen(not writable) in some cases (and I'm talking from production based experience).
这里实际上没有提到,并且可能是为什么不用window
作全局范围载体的最大问题是它在某些情况下可以被冻结(不可写)(我是从基于生产的经验中谈论的)。
A good pattern is actually just create a global variable that will be used for all the common stuff in your application
一个好的模式实际上只是创建一个全局变量,该变量将用于应用程序中的所有常见内容
var Application = {};
Application.state = { name: 'Application' }
.
.
What I found as the best pattern for me in javascript is to have a global state using Redux.
我发现在 javascript 中对我来说最好的模式是使用Redux拥有一个全局状态。
回答by Samy Bencherif
Another pattern to make a unique namespace in your application.
在应用程序中创建唯一命名空间的另一种模式。
function myApp()
{
var value = 5;
function addToValue(x)
{
value += x;
}
}()
If you want functions/values to be accessible afterwards you can store them in an object like this:
如果您希望之后可以访问函数/值,您可以将它们存储在这样的对象中:
function myApp()
{
this.value = 5;
this.addToValue = function(x)
{
this.value += x;
}
}
var myInstance = new myApp();