简体   繁体   English

JS中的局部和全局变量

[英]local and global variables in JS

I've the following javascript code 我有以下javascript代码

 var globalConfiguration = null;

    function loadFile(filePath) {   
    }

    function onLoadPage() {
    }

    function getConfiguration() {
    }

    function process() {
    }

I want to use IIFE to encolose all my functions in a closure to avoid cluttering the global name space,so I wrote : 我想使用IIFE将所有函数封装在一个闭包中,以避免混乱全局名称空间,因此我写道:

 (function(window){
    var globalConfiguration = null;

    function loadFile(filePath) {   
    }

    function onLoadPage() {
    }

    function getConfiguration() {
    }

    function process() {
    }
    });

However, I do not understand this notion very well, now in my HTML page how would I call my function onLoadPage ? 但是,我不太了解这个概念,现在在我的HTML页面中,我将如何调用函数onLoadPage

Now that you have enclosed the module, you need to decide what you want to expose to the outside world. 现在,您已经封闭了模块,您需要确定要向外界公开的内容。 Anything you want to expose, you can export . 您想公开的任何内容都可以导出 Also, you need to decide what context (in this case, window ) that you want to attach to. 另外,您需要确定要附加到的上下文 (在本例中为window )。 Then pass the context in right away (thus completing the IIFE). 然后立即传递上下文(从而完成IIFE)。

For example: 例如:

(function(window){
    var exports = {};
    var globalConfiguration = null;

    function loadFile(filePath) {   
    }

    function onLoadPage() {
    }

    function getConfiguration() {
    }

    function process() {
    }

    exports.getConfiguration = getConfiguration;
    window.myModule = exports;
})(window);

Attaching to the passed in window object is one way to export things out in a controlled fashion. 附加到传入的window对象是一种以受控方式导出内容的方法。 So, you do need to pass the context (in this case window) to the IIFE. 因此,您确实需要将上下文(在本例中为窗口)传递给IIFE。 Perhaps, window will not always be the context for the call. 也许, window并不总是调用的上下文。

After running this code, myModule will be available on the global scope. 运行此代码后, myModule将在全局范围内可用。

You can't without putting it in the global namespace somehow. 您不能以某种方式将其放在全局名称空间中。

My recommendation to structure code like this: 我建议构造这样的代码:

function ExampleHelper() {
    (function(scope) {

        scope.globalConfiguration = null;

        scope.loadFile = function(filePath) {

        };

        scope.onLoadPage = function() {

        };

        scope.getConfiguration = function() {

        };

        scope.process = function() {

        };

    })(this);
}

var foo = new ExampleHelper(); // "foo" now contains all of your functions
foo.onLoadPage();

You can set your function to window.onload callback. 您可以将函数设置为window.onload回调。

 (function(window) { var globalConfiguration = null; window.onload = onLoadPage; function loadFile(filePath) {} function onLoadPage() { alert('hello world'); } function getConfiguration() {} function process() {} }(window)); 

This is called chaining of functions/methods and is usually done for better readability of the code and to avoid the usage of temporary variables to hold the return value of each function. 这称为函数/方法链接,通常这样做是为了提高代码的可读性,并避免使用临时变量来保存每个函数的返回值。

Check this post on chaining methods which helped me to understand the chaining better. 查看有关链接方法的这篇文章,这有助于我更好地了解链接。

I see you wanted to use closures to avoid cluttering the global object. 我看到您想使用闭包来避免使全局对象混乱。 However, do note that we write functions for reusability. 但是,请注意,我们编写了可重用性的函数。 Though you create a closure, ensure that your methods inside the outer function are abstracted such that they can be used by other parts of the code. 尽管创建了一个闭包,但是请确保外部函数内部的方法是抽象的,以便它们可以被代码的其他部分使用。 For ex: from your code, you have a loadFile method which could be reused. 例如:从您的代码中,您有一个loadFile方法可以重用。

Now to see how you can use the methods you described in a chain. 现在来看一下如何使用链中描述的方法。

Assumptions: (since i don't know why you wrote those methods, i am making some assumptions). 假设:(由于我不知道您为什么编写这些方法,因此我做出了一些假设)。

  1. loadFile(filepath) - returns a file object loadFile(filepath)-返回文件对象
  2. onPageLoad() - once the page is loaded, it returns the object or id of the input file onPageLoad()-加载页面后,它将返回输入文件的对象或ID
  3. getConfiguration() - gets the file path getConfiguration()-获取文件路径
  4. process() - process the file process()-处理文件

onPageLoad().loadFile(getConfiguration()).process(); 。的onPageLoad()的loadFile(getConfiguration())处理();

The important part is that the scope of the object is set correctly in the chaining. 重要的部分是在链接中正确设置了对象的范围。 In order to do this, each method must return the reference to appropriate object. 为此,每个方法必须将引用返回到适当的对象。

Hope this helps. 希望这可以帮助。

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

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