类的构造函数不能没有新的调用

问题描述 投票:-1回答:1

我是新的面向对象编程和更多的NodeJS特殊,我已经尽力重定向路由到其它类,但我总是发现这个问题时,试图把这个路线

类型错误:类构造函数路由器不能没有援引“新”

我试过很多不同的方式,但它没有工作

//this is the fuction exists into the app.js

const index = require('./serverSide/router/index');
initRoutes() {
      app.use('/admin', index);
    app.use('/', (req, res) => {
        res.sendFile(__dirname + './src/index.html');
    });
    app.use(function (req, res, next) {
        const err = new Error('Not Found');
        err.status = 404;
        next(err);
    });
}

//and this is into the class Router

class Router {
    constructor(router) {
       console.log('/**Routing**/');
       router.get('/', this.result());
    }
    result(req, res) {
      console.log("works");
    }
 }

 module.exports = Router;
javascript node.js oop module
1个回答
-1
投票

你需要调用new一个类时,你包括constructor。欲了解更多信息点击这里:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

© www.soinside.com 2019 - 2024. All rights reserved.