我可以在这里使用JavaScript Closures而不是一个全局变量吗? [英] Can I use Javascript Closures here instead of a global variable?

查看:97
本文介绍了我可以在这里使用JavaScript Closures而不是一个全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前设定:

  var placeId; 
function selectPlace(place){
$('#selectPlace')。html('Selected Place:< b>'+ place.Name +'< / b&
$('#map')。hide(400);
placeId = place.Id;
}

$(document).ready(function()
{
$('#postMessage')。 alert(PlaceId:+ placeId);
});
});可以/应该使用闭包吗?


h2_lin>解决方案

根据您的意见,您似乎正在寻找的是:

  function selectPlace(place){
if(!place){
return selectPlace.placeId;
} else {
$('#selectPlace')。html('Selected Place:< b>'+ place.Name +'< / b>')
$('#map')。hide(400);
selectPlace.placeId = place.Id;
}
}

$(document).ready(function(){
$('#postMessage')。 b alert(PlaceId:+ selectPlace());
});
});

这不是使用闭包,它只是存储函数对象上最后分配的ID。然后,如果他们不使用该函数作为设置器,则返回该值。如果你想使用闭包做同样的事情,它看起来很像上面的例子:

 (function ){
var placeId;

window.selectPlace = function(place){
if(!place){
return placeId;
} else {
$('#selectPlace')。html('Selected Place:< b>'+ place.Name +'< / b>');
$('#map')。hide (400);
placeId = place.Id;
}
}
})();

顺便说一下,找到闭包的最简单方法是如果函数中有变量'在当前函数内部使用 var 来声明,但是在其它函数中已经存在。如上所示,变量 placeId 未在 selectPlace 函数中声明,意味着 selectPlace 函数是一个使用 placeId 变量的闭包。


Current setup:

var placeId;
function selectPlace(place) {
    $('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
    $('#map').hide(400);
    placeId = place.Id;
}

$(document).ready(function()
{
    $('#postMessage').click(function() {
        alert("PlaceId: " + placeId);
    });
});

Can/should I be using closures?

解决方案

Based on your comments, it seems like what you're looking for is this:

function selectPlace(place) {
  if(!place){
    return selectPlace.placeId;
  }else{
    $('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
    $('#map').hide(400);
    selectPlace.placeId = place.Id;
  }
}

$(document).ready(function(){
  $('#postMessage').click(function() {
    alert("PlaceId: " + selectPlace());
  });
});

This isn't using a closure, it just stores the last assigned ID on the function object. Then you'd return the value if they don't use the function as a setter. If you wanted to use a closure to do the same thing, it would look a lot like the example above:

(function(){
  var placeId;

  window.selectPlace = function(place) {
    if(!place){
      return placeId;
    }else{
      $('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
      $('#map').hide(400);
      placeId = place.Id;
    }
  }
})();

By the way, the easiest way to spot a closure is if a function has variables in it that haven't been declared with var inside of the current function, but have been in some other function it sits inside. As you can see above, the variable placeId isn't declared inside of the selectPlace function, meaning that the selectPlace function is a closure that uses the placeId variable.

这篇关于我可以在这里使用JavaScript Closures而不是一个全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆