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.