我需要导出一个递归函数。从函数中引用 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/