zoukankan      html  css  js  c++  java
  • An Example for Javascript Function Scoping and Closure

    1. An Real World Example

    In the patron detail page of the CRM system I'm working with, there’re large amount of data. To shorten user’s waiting time, we want to only load basic information at page loaded then dynamically load the others in ajax.

    Now we’re going to call ajax in a “for” loop:

    var subpanelIDs = ['panel1', 'panel2', 'panel3'];
    function loadSubpanels() {
       for (var i = 0; i < subpanelIDs.length; i++) {
            var panelID = subpanelIDs[i];
            var successFunc = function (resultHtml) {
                    console.log(panelID);  //!!!It console 'panel3’ for 3 times
                    //$('#'+panelID).html(resultHtml);
            };
            $.ajax({
                url: url,
                dataType: "html",
                success: successFunc
            });
        }
    }
    loadSubpanels();
    In the success call back, we want to find element with an id “panelID” and fill the html to it. But unluckily the panelID is always “panel3”.

    2. Function Scoping in Javascript

    It’s about function scoping and closure.

    Javascript is a lexical scoping language, also called static scoping, that means if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to the variable's value, but outside that text, the variable name does not exist.

    In our example, panelID’s scope is the text of whole function loadSubpanels. The 3 times of invoking “successFunc” are within a single invoking of “loadSubpanels”, so the current scope chain is shared by 3 “successFunc”. And result is that they all getting the same value of ‘panel3’.

    Here we’re not going to explain detail for scoping and closure, you can refer to the book “javascript.the.definitive.guide”

    3. Resolution

    To avoid the 3 invoking of “successFunc” sharing same scope chain, we need to seperate them into another function “loadOnePanel”.

    In this case each loadOnePanel will own a scope chain the the value of panelID will not change unexpectedly. The call back function now can fill 3 panels accrodingly.

    var subpanelIDs = ['panel1', 'panel2', 'panel3'];
    function loadSubpanels() {
       for (var i = 0; i < subpanelIDs.length; i++) {
            loadOnePanel(subpanelIDs[i]);
        }
    }
    function loadOnePanel(panelID){
        var successFunc = function (resultHtml) {
            console.log(panelID);
        };
        $.ajax({
            url: url,
            dataType: "html",
            success: successFunc
        });
    }
    loadSubpanels();
  • 相关阅读:
    axios
    vue-cli-service 报错
    避免大型、复杂的布局和布局抖动
    vue 父子通信
    == 区别 === ,!= 区别 !==
    全选/取消全选
    vue 注意
    pyparsing:自定义一个属于你的语法解析器(更新中)
    《python解释器源码剖析》第11章--python虚拟机中的控制流
    collections:内建模块,提供额外的集合类
  • 原文地址:https://www.cnblogs.com/hiteddy/p/An_Example_on_Closure_and_Javascript_function_scope.html
Copyright © 2011-2022 走看看