使用递归函数时如何避免模块之间的循环依赖?

How to avoid circular dependency between modules when working with recursive functions?

以下代码遍历一个对象及其键。它根据每个键所引用的值的类型为每个键做一些事情。它运作良好。一切都很好。

const obj = {
  prop1: {
    prop2: 1,
    prop3: {
      prop4: 2
    }
  },
  prop5: "1"
}

function inspectRecursively(node) {
  switch (typeof node) {
    case "object":
      handleObject(node);
      break;
    case "string":
      handleString(node);
      break;
    case "number":
      handleNumber(node);
      break;
    default:
      break;
  }
}

function handleObject(node) {
  console.log(JSON.stringify(node, null, 2))
    
  for (const [key] of Object.entries(node)) {
    inspectRecursively(node[key]);
  }
}

function handleString(node) {
  console.log(node)
}

function handleNumber(node) {
  console.log(node.toString().padStart("0", 3))
}

inspectRecursively(obj)

说我认为文件太大了。我把它分成模块

main.js

import { importRecursively } from "./importRecursively"

const obj = {
  prop1: {
    prop2: 1,
    prop3: {
      prop4: 2
    }
  },
  prop5: "1"
}
 inspectRecursively(obj)

importRecursively.js

import { handleObject } from "./handleObject.js"
import { handleString} from "./handleString.js"
import { handleNumber} from "./handleNumber.js"

function inspectRecursively(node) {
  switch (typeof node) {
    case "object":
      handleObject(node);
      break;
    case "string":
      handleString(node);
      break;
    case "number":
      handleNumber(node);
      break;
    default:
      break;
  }
}

handleObject.js

import { importRecursively } from "./importRecursively"

function handleObject(node) {
  console.log(JSON.stringify(node, null, 2))
    
  for (const [key] of Object.entries(node)) {
    inspectRecursively(node[key]);
  }
}

handleString.js

function handleString(node) {
  console.log(node)
}

handleNumber.js

function handleNumber(node) {
  console.log(node.toString().padStart("0", 3))
}

现在我遇到了循环依赖。

main -> inspectRecursively -> handleObject -> importRecursively

我认为这很糟糕,但不确定吗?

遇到这种情况我该怎么办?我是否要更改某些内容以避免循环依赖?

我会把Inspect写成自己的模块-

// Inspect.js

const inspect = v =>
  // ...

const handleString = e =>
  // ...

const handleObject = e =>
  // ...

const handleNumber = e =>
  // ...

export { inspect } // only export functions the user should call

我真的不明白将每个 handle* 分成自己的文件的目的。

现在当你在你的程序中使用它时 -

import { inspect } from './Inspect'

inspect(someObj) // => ...

I think this is bad, but am not sure about it?

不,还不错。 ES6 模块确实可以很好地处理这样的循环依赖。只要确保所有这些模块都是纯的并且只包含函数声明,而不是 top-level 构造依赖于导入值的值的代码:声明是跨模块“提升”的。

Do I change something to avoid the circular dependency?

您可以使用显式依赖注入:

// inspectRecursively.js
import { makeHandleObject } from "./handleObject.js"
import { handleString} from "./handleString.js"
import { handleNumber} from "./handleNumber.js"

const handleObject = makeHandleObject(inspectRecursively);

function inspectRecursively(node) {
  …
}
// handleObject.js

export function makeHandleObject(inspectRecursively) {
  return function handleObject(node) {
    …
  };
}