javascript - Node : Calling an exported function from within the declaring module

标签 javascript node.js recursion

我需要导出一个递归函数。从函数中引用 exports 对象可以吗? (我担心循环引用)。

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 ?
    });
  }
}

最佳答案

好的,它可以工作,但是有一个更简洁的解决方案:

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);
    });
  }
}

关于javascript - Node : Calling an exported function from within the declaring module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351964/

相关文章:

javascript - 让两个模型显示在页面上

javascript - 计算属性未在 aurelia 中更新

javascript - delay() 在 each() 循环中没有按预期工作 (jQuery)

node.js - hapi 错误 : handler method did not return a value, 一个promise,或者抛出一个错误

javascript - 关于connection.end()在node.js mySQL模块中如何工作的困惑

javascript - 数组的 Promise 未按预期工作

c - 生成数字的所有不同分区

java - 寻找递归解的迭代解

javascript - Titanium Javascript 运行时错误

python - 我在 python 中的正则表达式没有正确递归