如何在javascript中声明变量

1 javascript jquery

$("#btnsub").click(function () {
    var add = $("#txtname").val();

    var obj;
    var geo = new google.maps.Geocoder;
    geo.geocode({ 'address': add }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            obj = results[0].geometry.location;
            obj = convert(obj);
            alert(obj);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
    alert(obj);
  // why this alert run first instead of the upper alert()
});
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 5

geocode函数是异步的.

您传递的回调在调用后运行一段时间geocode; 该geocode函数本身不会(也不能)等待响应.