我可以在这里使用Javascript闭包而不是全局变量吗?
当前设置:我可以在这里使用Javascript闭包而不是全局变量吗?,javascript,closures,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').click(funct
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);
});
});
var-placeId;
功能选择地点(地点){
$('#selectPlace').html('selectPlace:'+Place.Name+'');
$('#map').hide(400);
placeId=place.Id;
}
$(文档).ready(函数()
{
$('#postMessage')。单击(函数(){
警报(“PlaceId:+PlaceId”);
});
});
我可以/应该使用闭包吗?这似乎是一件合理的事情,根据上下文,您可以通过用函数表达式替换代码轻松做到:
(function(){
var placeId;
// It looks like you want selectPlace to be a global function?
// hence i'm using assignment of a function expression here
selectPlace = function (place) {
$('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
$('#map').hide(400);
placeId = place.Id;
}
$(document).ready(function()
{
$('#postMessage').click(function() {
alert("PlaceId: " + placeId);
});
});
})();
(函数(){
砂质变种;
//看起来你想让selectPlace成为一个全局函数?
//因此,我在这里使用函数表达式的赋值
selectPlace=函数(place){
$('#selectPlace').html('selectPlace:'+Place.Name+'');
$('#map').hide(400);
placeId=place.Id;
}
$(文档).ready(函数()
{
$('#postMessage')。单击(函数(){
警报(“PlaceId:+PlaceId”);
});
});
})();
根据您的评论,您想要的似乎是:
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());
});
});
功能选择地点(地点){
如果(!地点){
返回selectPlace.placeId;
}否则{
$('#selectPlace').html('selectPlace:'+Place.Name+'');
$('#map').hide(400);
选择place.placeId=place.Id;
}
}
$(文档).ready(函数(){
$('#postMessage')。单击(函数(){
警报(“PlaceId:+selectPlace());
});
});
这不是使用闭包,它只是在函数对象上存储最后分配的ID。如果他们不将函数用作setter,则返回该值。如果您想使用闭包来做同样的事情,它看起来很像上面的示例:
(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;
}
}
})();
(函数(){
砂质变种;
window.selectPlace=功能(位置){
如果(!地点){
返回placeId;
}否则{
$('#selectPlace').html('selectPlace:'+Place.Name+'');
$('#map').hide(400);
placeId=place.Id;
}
}
})();
顺便说一句,发现闭包的最简单方法是,如果函数中的变量没有在当前函数中用
var
声明,而是在它所在的其他函数中声明。正如您在上面看到的,变量placeId
没有在selectPlace
函数中声明,这意味着selectPlace
函数是一个使用placeId
变量的闭包。我想我当时没有得到它(闭包)。我想我可以使用它们来基本上不使用var placeId,而不是像selectPlace()那样返回一个place.Id。比如:selectPlace=function(place){$('#selectPlace').html('selectPlace:'+place.Name+'');$('#map')。hide(400);return place.Id;}$('#postMessage')。单击(function(){alert)(PlaceId:+selectPlace();});这不起作用:P我也不确定你的示例如何比我的代码更好?我不想听起来像个屁股,但我真的不知道。我不是一个javascript人,所以不确定这样做会有什么帮助或更好?如果没有你所做的全部内容,我无法确定你的意图,但是在这种情况下使用闭包(函数表达式的)是作用于作用域placeId,从而防止它污染全局作用域。非常酷。是的,我猜我不需要闭包,只是试图避免使用全局(听说它们不好)。不知道为什么我想不起来,但感谢您添加它!而不是警报(“placeId:+placeId())你是说警报(“PlaceId:+selectPlace());?