koa-router 中的 new Router() 和 Router() 是否相同?

is new Router() and Router() identical in koa-router?

我看到使用 koa-router 的代码如下。

const Router = require('koa-router')

const routerWithoutNew = Router()

我以为这是一个错误,但我很惊讶地看到它工作正常。

const Router = require('koa-router')

const routerWithNew = new Router()

而且,它也很好用。

routerWithoutNewrouterWithNew 有什么区别?

由于 Koa Router 源代码中的以下行,它们被相同地对待:

if (!(this instanceof Router)) return new Router(opts);

(来自 https://github.com/koajs/router/blob/56735f009768e32cce89af60337e7e2a8d6bbbc4/lib/router.js#L51