简体   繁体   English

在JavaScript“窗口”对象中存储变量是使用该对象的正确方法吗?

[英]Storing a variable in the JavaScript 'window' object is a proper way to use that object?

(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. (也许)我刚刚通过在JavaScript window对象中“存储”/“保存”变量解决我的问题( 如何在从对话窗口成功提交表单之后更新前端内容? )。 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 . 但是,由于我是JavaScript的新手, 我有一些疑问,如果在JavaScript window对象中存储/保存变量是使用该对象的“常见”/“正确”方式 Is it? 是吗?

For example, using the following code 例如,使用以下代码

$('.trigger').click(function() {
  window.trigger_link = this;
});

is advisable? 是明智的吗?

In JavaScript, any global variable is actually a property of the window object. 在JavaScript中,任何全局变量实际上都是window对象的属性。 Using one is equivalent to (and interchangeable with) using the other. 使用一个相当于使用另一个(并且可以互换)。

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. 另一种方法是使用.data()将变量附加到相关的DOM元素。 This is not practical in all cases, but it's a good way to get variables that can be accessed globally without leaving them in the global namespace. 这在所有情况下都不实用,但它是获取可以全局访问的变量而不将它们留在全局命名空间中的好方法。

What is actually not mentioned here, and is probably the biggest deal breaker on why not to use window as global scope carrier is 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建立全局状态。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM