为什么instanceof返回false?

时间:2017-06-07 15:22:02

标签: javascript node.js babeljs

Server.js

import Router from '@core/router/Router'

class Server {
  constructor () {
  }


  use (router) {
    if ((router instanceof Router) === false) {
      throw Error('You can only use a Router instance with Server::use')
    }

    this.router = router
  }
}

export default Server

app.js

import Server from '@core/Server/Server'
import router from '@auth/router'

var app = new Server()

app.use(router)

export default app

router.js

import Router from '@core/Router/router'

var router = new Router([
])

export default router

我得到了

  

错误:您只能使用Server :: use的Router实例       在Server.use(C:\ Users \ tom.cordelier \ Desktop \ chacha-emu \ lib \ core \ Server \ Server.js:89:15)       在对象。 (C:\ Users \用户tom.cordelier \桌面\茶茶-鸸鹋\ lib中\ AUTH \ app.js:21:5)       在Module._compile(module.js:571:32)       at Object.Module._extensions..js(module.js:580:10)       在Module.load(module.js:488:32)       在tryModuleLoad(module.js:447:12)       在Function.Module._load(module.js:439:3)       在Module.runMain(module.js:605:10)       在运行时(bootstrap_node.js:427:7)       在启动时(bootstrap_node.js:151:9)

看起来instanceof无法正常工作,但router.constructor.name很好。

我的.babelrc

{
  "presets": [
    "env",
    "stage-2",
    "stage-3"
  ],
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "alias": {
        "@core": "./src/core",
        "@auth": "./src/auth",
      }
    }]
  ]
}

我想我很想理解导出默认值。 谢谢你帮助我!

0 个答案:

没有答案