简体   繁体   English

变量函数未定义错误

[英]variable function undefined error

i have this two functions, inside one function. 我有这两个功能,在一个功能内。

var funcA = function(){
  var funcB = function(){
    // function codes
    funcC();
  }

  var funcC = function(){
    // function codes
    funcB();
  }
}

funcA is executed first, then when I call funcB it produce error Object [object global] has no method 'funcC' . 首先执行funcA,然后当我调用funcB时它产生错误Object [object global] has no method 'funcC' if i put funcC right before funcB the error is now pointing to undefined funcB . 如果我在funcC之前funcB则错误现在指向undefined funcB

how to fix this problem? 如何解决这个问题?

Your problem is in the way you're declaring functions: 你的问题在于你宣布功能的方式:

var funcB = function(){
    // function codes
    funcC();
  }

var funcC = function(){
    // function codes
    funcB();
}

When you're declaring funcB , it tries to find funcC , which doesn't exist, yet. 当你声明funcB ,它会尝试找到不存在的funcC This breaks your code, throwing an undefined error. 这会破坏您的代码,抛出一个undefined错误。

You can define the functions like this: 您可以像这样定义函数:

function funcB(){
    // function codes
    funcC();
  }

function funcC(){
    // function codes
    funcB();
}

However, if funcB calls funcC , and funcC calls funcB , it doesn't matter how they're declared / initialized, you're going to have an infinite loop, probably resulting in a stack overflow-like error, or a crashing browser (/tab). 但是,如果funcB调用funcC ,并且funcC调用funcB ,那么它们如何被声明/初始化并不重要,你将会有一个无限循环,可能导致类似堆栈溢出的错误,或者崩溃的浏览器( /标签)。

Chrome throws this: Chrome会抛出这个:

RangeError: Maximum call stack size exceeded

To avoid that error, do something like this: 要避免该错误,请执行以下操作:

function funcB(calledFromC){
    if(!calledFromC){
        funcC(true);
    }
}

function funcC(calledFromB){
    if(!calledFromB){
        funcB(true);
    }
}

Or shorter: 或更短:

function funcC(calledFromB){
    !calledFromB && funcB(true);
}

If you would declare functions like this: 如果你要声明这样的函数:

var funcA = function(){
   function funcB() {
      funcC();
   }

   function funcC {
      funcB();
   }
}

it would work. 它会奏效。 Why you ask? 你为什么问? Read more about hoisting, scoping, function declarations and function expression here 在此处阅读有关提升,范围,函数声明和函数表达式的更多信息

check fiddle here 在这里检查小提琴

of-course it would be an infinite loop, so don't do this, but it would be correct javascript. 当然它将是一个无限循环,所以不要这样做,但它将是正确的JavaScript。

edit: if you would like to call them out of the scope (outside of funcA), you need to return them: 编辑:如果你想在范围外调用它们(在funcA之外),你需要返回它们:

 var funcA = (function() {
    var funcB = function () {
        //code here
    };
    var funcC = function () {
        //code here
    };
    return {
        funcB: funcB,
        funcC: funcC
    };
   })();

Now you can do this outside the funcA: 现在你可以在funcA之外做到这一点:

funcA.funcB();
funcA.funcC();

You are probably asking the wrong question as interlinked methods rarely end happily. 您可能会提出错误的问题,因为相互关联的方法很少会愉快地结束。

The answer to your question really is because of this earlier answer regarding when functions actually exist . 你的问题的答案实际上是因为这个早期的答案关于函数何时存在 In particular read about function hoisting. 特别是关于功能提升的内容。

var funcA = function() {

  function funcB(){
    // function codes
    funcC();
  }

  function funcC() {
    // function codes
    funcB();
  }
}

funcA.funcB();

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

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