javascript javascript返回值总是未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11441759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:13:29  来源:igfitidea点击:

javascript return value is always undefined

javascript

提问by Mustafa ELnagar

I'm trying to retrieve the 2 attributes of seprated function and I debug there values before the end of the function and they have a value but the return value is alwas undifined I don't know why !!

我正在尝试检索分离函数的 2 个属性,并在函数结束之前调试这些值,它们有一个值,但返回值始终未定义,我不知道为什么!!

the .js file

.js 文件

function STAAPlanlat(){
  alert ("the function");

  if (navigator.geolocation) {
    //we supposed to call the handlers of the expections 
    navigator.geolocation.watchPosition(function(position) {
      alert("show position ");
     //  x.innerHTML="Latitude: " + position.coords.latitude +"<br />Longitude: " + position.coords.longitude;  
      var lat=position.coords.latitude;
      var lan=position.coords.longitude;    

      //x.innnerHTML=out
      alert(lat+"<"+lan);
      return lan;
    });

  } else {
    alert("error");
  }
}

I got the alert with the values of the lan and lat

我收到了 lan 和 lat 值的警报

but when I call on separated file it return undefined return value

但是当我调用分离的文件时,它返回未定义的返回值

 <!DOCTYPE html>
 <html>
 <head>
     <script type="text/javascript" src="STAAP1.2.js"> </script>

 <script type="text/javascript">
     function test(){
     var out=STAAPlanlat();     
     document.getElementById("STAAPlanlat").value = "lan is"+out;
     //document.writeln("lan is"+out);
     }
     </script>  
 </head>
 <body>
 <p id="STAAPlanlat">Test the division</p>
 <button onclick="test()">STAAPlanlat()</button>
 <button onClick="alertme()" >Alert</button>

 </body>
 </html>

回答by iurisilvio

You are returning in the anonymous function and this value is never assigned to anything. You can do what you want with a callback.

您正在匿名函数中返回,并且这个值永远不会分配给任何东西。你可以用回调做你想做的事。

// untested code, hope it works
function STAAPlanlat(callback){
    alert ("the function");
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(function(position) {
            var lat=position.coords.latitude;
            var lan=position.coords.longitude;  
            callback(lat, lan);
        });
    }
    else{
        alert("error");
    }
}

And your test function...

而你的测试功能...

function test(){
    var out;
    STAAPlanlat(function(lat, lon) { out = lat; });
}

回答by jeschafe

Cause you're not returning it from the main function, you're returning it from the embedded anonymous function which isn't doing anything with it. Do this:

因为您不是从主函数返回它,而是从嵌入的匿名函数返回它,该函数对它没有做任何事情。做这个:

function STAAPlanlat(){
var lat;
var lan;
alert ("the function");
if (navigator.geolocation) {
    //we supposed to call the handlers of the expections 
    navigator.geolocation.watchPosition(function(position) {
        alert("show position ");
        //  x.innerHTML="Latitude: " + position.coords.latitude +"<br />Longitude: " + position.coords.longitude;   
        lat=position.coords.latitude;
        lan=position.coords.longitude;  

        //x.innnerHTML=out
        alert(lat+"<"+lan);
    });
    return lan;
}
else
    alert("error");
}

回答by jagm

because function STAAPlanlat doesn't return any value. your anonymous function returns lanbut it is asynchronous callback. add this before return lan;:

因为函数 STAAPlanlat 不返回任何值。您的匿名函数返回lan但它是异步回调。在此之前添加return lan;

document.getElementById("STAAPlanlat").value = "lan is" + lan;