简体   繁体   English

节点:从声明模块中调用导出的函数

[英]Node: Calling an exported function from within the declaring module

I need to export a recursive function. 我需要导出一个递归函数。 Is it okay to refer to the exports object from within a function? 从函数中引用exports对象可以吗? (I'm worried about the circular reference). (我担心循环参考)。

exports.traverse = function(node, cb){
  if(node.hasOwnProperty("value")){
    cb(node.value);
  }else if(node.hasOwnProperty("children")){
    node.children.forEach(function(child){
      exports.traverse(child, cb);  // Err, is this ok ?
    });
  }
}

It is OK, it works, but there's a cleaner solution: 确定 ,它的工作原理,但有一个清洁的解决方案:

exports.traverse = function traverse(node, cb){
  if(node.hasOwnProperty("value")){
    cb(node.value);
  }else if(node.hasOwnProperty("children")){
    node.children.forEach(function(child){
      traverse(child, cb);
    });
  }
}

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

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