用Javascript返回值

用Javascript返回值

问题描述:

我有一个(希望非常简单)的Javascript问题。我搜索过但没有发现与问题真正相关的内容。用Javascript返回值

基本上我有一个函数(addToGlobe),它在运行时调用两个其他函数(codeAddressLat和codeAddressLng)。这两个被调用的函数都应该返回一个浮点值给第一个函数,然后使用它们。子函数的工作是正确的 - 我做了一个print语句来检查每个函数中的“numfinal”变量是否有值,并且它的确如此。

但是,当我向调用函数添加打印语句(如代码中所述)时,它返回'undefined'。因此,问题似乎是在返回numfinal值时。

谢谢:)

function addToGlobe(uname, uid, pmcity) { 
    // Get lat & long of city 
    var pmlat = codeAddressLat(pmcity); 
    var pmlong = codeAddressLng(pmcity); 

    log(pmlat); // PROBLEM! Prints 'undefined' 
    log(pmlong); // PROBLEM! Prints 'undefined' 

    // Rest of function removed to keep it simple 
} 

function codeAddressLat(inputcity) { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 

    geocoder.geocode({ 'address': inputcity}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var llsplit = new Array(); 

      bkresult = String(results[0].geometry.location); 
      bkresult = bkresult.replace(/[\(\)]/g, ""); 
      llsplit = bkresult.split(','); 

      numfinal = parseFloat(llsplit[0]); 
      return numfinal; 

     } else { 
     log('<b><font color="#C40031">Geocode was not successful:</b> ' + status); 
     } 
    }); 
} 

    function codeAddressLng(inputcity) { 
     // Basically the same function as above. Removed for simplicity 
    } 

codeAddressLat实际上并没有返回任何东西。它传递给geocoder.geocode的匿名函数是。

由于geocoder.geocode异步运行,因此codeAddressLat不能等待其答案。所以codeAddressLat真的不能返回任何有价值的东西。相反,codeAddressLat也需要变得异步。这是JavaScript中的一种常见模式。

function addToGlobe(uname, uid, pmcity) { 
    codeAddressLat(pmcity, function(pmlat) { 
     // do something with pmlat 
    }); 

    ... 
} 

function codeAddressLat(inputcity, callback) { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 

    geocoder.geocode({ 'address': inputcity}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var llsplit = new Array(); 

      bkresult = String(results[0].geometry.location); 
      bkresult = bkresult.replace(/[\(\)]/g, ""); 
      llsplit = bkresult.split(','); 

      numfinal = parseFloat(llsplit[0]); 

      // instead of returning, call the callback with the result 
      callback(numfinal); 

     } else { 
     log('<b><font color="#C40031">Geocode was not successful:</b> ' + status); 
     } 
    }); 
} 
+0

很好,非常感谢! :) – 2011-04-05 17:37:41

您没有return语句在codeAddressLat,你有一个内部codeAddressLat定义的回调函数内。

您的函数codeAddressLat实际上并不是返回一个值,而是调用传递一个正在返回值的回调函数的函数。您需要等到地理编码操作完成才能检索numfinal的值。

您的函数codeAddressLat根本没有返回任何值,因此您将得到未定义的输出。

+0

codeAddressLat根本没有返回任何东西(这意味着它默认返回undefined)。 return语句不在codeAddressLat本身中。 – 2011-04-05 17:16:40

+0

@Matt - 是的,你是对的。错过了看到封闭的功能。谢谢 – 2011-04-05 17:19:19

codeAddressLat方法调用中的地理编码请求不会将值返回给调用方。你的回报是不同的范围。

这是行不通的吗?

function codeAddressLat(inputcity) { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 

    return geocoder.geocode({ 'address': inputcity}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var llsplit = new Array(); 

      bkresult = String(results[0].geometry.location); 
      bkresult = bkresult.replace(/[\(\)]/g, ""); 
      llsplit = bkresult.split(','); 

      numfinal = parseFloat(llsplit[0]); 
      return numfinal; 

     } else { 
     log('<b><font color="#C40031">Geocode was not successful:</b> ' + status); 
     } 
    }); 
} 

geocoder.geocode功能看起来是异步的。您的codeAddressLatcodeAddressLng函数在geocoder.geocode函数从服务器获取数据之前返回void。

解决此问题的一种方法是将您的呼叫嵌套到geocoder.geocode并使用变量范围设定,以便所有AJAX呼叫都返回后,您可以调用addToGlobe函数传递所需的两个参数。

事情是这样的:

codeAddressLatAndLong(pmcity); 

function addToGlobe(pmlatlat, pmlatlong) { 
    log(pmlat); // PROBLEM! Prints 'undefined' 
    log(pmlong); // PROBLEM! Prints 'undefined' 

    // Rest of function removed to keep it simple 
} 

function codeAddressLatAndLong(inputcity) { 
    // stuff 

    geocoder.geocode({ 'address': inputcity}, function(results, status) { 
     // stuff goes here 
     pmlat = parseFloat(llsplit[0]); 
     geocoder.geocode({...}, function(results, status) { 
     // more stuff 
     pmlatlong = something; 
     addToGlobe(pmlatlat, pmlatlong); 
     }); 
    }); 
} 

欢迎AJAX的世界。