简体   繁体   English

为什么带有参数的返回函数未定义?

[英]Why return function with arguments is undefined?

I tried for many hours but I don't understand why return is undefined... 我尝试了很多小时,但我不明白为什么return是不确定的...

This is my code: 这是我的代码:

var x = 30.858;
var id = 584;

var num_reg = region(x);
if (num_reg == false) {
  num_reg = ripeti_region(x);
}


function ripeti_region(lng) {
  if (lng <= 180) {
    lng = lng - 0.2;
  } else if (lng => -180) {
    lng = lng + 0.2;
  }

  if (region(lng) != false) {
    var x = region(lng);   
    return x;
  } else {
    ripeti_region(lng);
  }
}


function region(lng) {
  if (lng <= 30.458) {
   return id;
  } else {
   return false;
  }
}


alert(num_reg);

If I alert num_reg I have undefined return, but the code works in fact in function ripeti_region(lng) if you add alert(x) it prints var id 如果我警告num_reg我有未定义的返回值,但是如果您添加alert(x) ,代码实际上可以在函数ripeti_region(lng)工作,它会打印var id

So I would that num_reg give like output var id value. 因此,我希望num_reg给出类似输出var id值。

This is jsfiddle: https://jsfiddle.net/nj4duw61/ 这是jsfiddle: https ://jsfiddle.net/nj4duw61/

Your problem is here: 您的问题在这里:

else {
    ripeti_region(lng);
  }

This part of code doesn't return anything. 这部分代码不返回任何内容。

Probably you wanted to return ripeti_region(lng); 可能您想return ripeti_region(lng);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM