JavaScript 中的闭包

发布于 2024-11-09 13:02:10 字数 1041 浏览 0 评论 0原文

function initialize(final) 
{
    if (GBrowserIsCompatible()) {
             ................................................
        }

        var address_array = final.split('~');

        for (var count = 0; count < address_array.length; count++) {
            if (geocoder) {
                geocoder.getLatLng(
        address_array[count],
        makeTheFunction(address_array, count)
    );
            }
        }

  }



  function makeTheFunction(array, thisCount) {
      return function (point) {
          if (!point) {
              alert(array[thisCount] + " not found");
          }


          else {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      };
  }

我的问题是,我无法从 else 部分访问 array[thisCount],尽管可以从 if 块访问它。即 alert(array[thisCount] + " not find") ; 正在工作 请帮忙

function initialize(final) 
{
    if (GBrowserIsCompatible()) {
             ................................................
        }

        var address_array = final.split('~');

        for (var count = 0; count < address_array.length; count++) {
            if (geocoder) {
                geocoder.getLatLng(
        address_array[count],
        makeTheFunction(address_array, count)
    );
            }
        }

  }



  function makeTheFunction(array, thisCount) {
      return function (point) {
          if (!point) {
              alert(array[thisCount] + " not found");
          }


          else {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      };
  }

my problem is tht i m not able to access the array[thisCount] from the else section although its accessible from the if block.. i.e alert(array[thisCount] + " not found"); is working
please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2024-11-16 13:02:10

else 块或“click”处理程序中是否无法访问它?如果您无法仅在“单击”处理程序中获取 array/thisCount,您是否尝试过复制这些变量?这可能是上下文的问题吗?如果您的数组在 else 块中可见,请尝试此操作:

function makeTheFunction(array, thisCount) {
  return function (point) {
      if (!point) {
          alert(array[thisCount] + " not found");
      }


      else {
          var item = array[thisCount];
          var marker = new GMarker(point);
          map.addOverlay(marker);
          GEvent.addListener(marker, "click", function () {
              marker.openInfoWindowHtml(item + "</b>");
          });
      }
  };

}

Is it inaccessible within else block or within “click” handler? If you cannot get array/thisCount only within your “click” handler, have you tried to copy these variables? Can it be an issue with contexts? Try this, if your array is visible within else block:

function makeTheFunction(array, thisCount) {
  return function (point) {
      if (!point) {
          alert(array[thisCount] + " not found");
      }


      else {
          var item = array[thisCount];
          var marker = new GMarker(point);
          map.addOverlay(marker);
          GEvent.addListener(marker, "click", function () {
              marker.openInfoWindowHtml(item + "</b>");
          });
      }
  };

}

烟凡古楼 2024-11-16 13:02:10
function makeTheFunction(array, thisCount) 
{         
          if (!point) 
          {
              alert(array[thisCount] + " not found");
          }
          else 
          {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      return point;
  }
function makeTheFunction(array, thisCount) 
{         
          if (!point) 
          {
              alert(array[thisCount] + " not found");
          }
          else 
          {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      return point;
  }
今天小雨转甜 2024-11-16 13:02:10

我建议 addListener 函数出了问题。在提供的代码中,侦听器中的 thisCount 应该有一个对 makeTheFunction 函数中的 thisCount 的闭包。

以下模拟发布的代码:

<script type="text/javascript">

function init() {
  var count = 'the count';
  partTwo(makeFn(count));

  function makeFn(thisCount) {
    return function() {

      // Shows 'thisCount: the count'
      alert('thisCount: ' + thisCount);
      document.getElementById('btn0').addEventListener('click',
        function(){alert('thisCount: ' + thisCount);}, false);
    }
  }
}

function partTwo(fn) {
  fn();
}

window.onload = function() {
  init();
};

</script>

<!-- Shows 'thisCount: the count' -->   
<button id="btn0">Button 0</button>

但是,它使用浏览器 addEventListener 附加侦听器,而不是明显自定义的 addListener

I suggest that there is something going wrong in the addListener function. In the code provided, thisCount in the listener should have a closure to thisCount in the makeTheFunction function.

The following emulates the posted code:

<script type="text/javascript">

function init() {
  var count = 'the count';
  partTwo(makeFn(count));

  function makeFn(thisCount) {
    return function() {

      // Shows 'thisCount: the count'
      alert('thisCount: ' + thisCount);
      document.getElementById('btn0').addEventListener('click',
        function(){alert('thisCount: ' + thisCount);}, false);
    }
  }
}

function partTwo(fn) {
  fn();
}

window.onload = function() {
  init();
};

</script>

<!-- Shows 'thisCount: the count' -->   
<button id="btn0">Button 0</button>

However it attaches the listener using the browsers addEventListener, not the apparently custom addListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文