简体   繁体   English

JavaScript super.constructor() 抛出“访问此之前必须调用”异常

[英]JavaScript super.constructor() throws "must call before accessing this" exception

I'm trying to derive a class in JavaScript.我正在尝试在 JavaScript 中派生一个类。 Basically I have a class Auth (more like an interface) and a class UserLogin which extends from it.基本上我有一个类Auth (更像是一个接口)和一个从它扩展的类UserLogin The problem is I get this exception even though I'm calling super.constructor() before anything else:问题是即使我先调用super.constructor() ,我也会收到此异常:

/Users/octav/Documents/work/framework-one/framework/server/src/user_login.js:12
        super.constructor(server);
        ^

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
    at new UserLogin (/Users/octav/Documents/work/framework-one/framework/server/src/user_login.js:12:3)
    at Object.<anonymous> (/Users/octav/Documents/work/framework-one/framework/server/app.js:8:14)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

The base class:基类:

/*
 * Generic authentication class
 */

const ServerError = require("./server_error.js");

class Auth {

    constructor(server) {
        if (server === undefined || typeof("server") !== "object")
            throw new ServerError("Bad type or missing argument 'server'");
        this.server = server;
    }
...

The UserLogin class - as you can see I'm not calling this before calling the super constructor. UserLogin类 - 如您所见,在调用超级构造函数之前我没有调用this

/*
 * User Login class
 */

const Auth = require("./auth.js");

const ServerError = require("./server_error.js");

class UserLogin extends Auth {

    constructor(server, config) {
        super.constructor(server); // <== This is where the error is triggered

        if (config === undefined || typeof(config) !== "object")
            throw new ServerError("Missing config or bad type");
        if (config.endpoints === undefined || typeof(config.endpoints) !== "object")
            throw new ServerError("Missing config.endpoints or bad type");
        if (config.endpoints.login === undefined || typeof(config.endpoints.login) !== "object")
            throw new ServerError("Missing config.endpoints.login or bad type");
        if (config.endpoints.logout === undefined || typeof(config.endpoints.logout) !== "object")
            throw new ServerError("Missing config.endpoints.logout or bad type");

        this.endpoints = config.endpoints;
    }
...

My start-up script:我的启动脚本:

const Server = require("./src/server.js");
const UserLogin = require("./src/user_login.js");

const config = require("./config.json");

const server = new Server(config);

const auth = new UserLogin(server, config.login); // <== This is where the error is triggered

server.setAuth(auth);

server.start();

I tried to look for answers online but all I could find were pages about how to derive classes in JavaScript.我试图在网上寻找答案,但我能找到的只是关于如何在 JavaScript 中派生类的页面。

Any ideas?有任何想法吗?

Inside the constructor of UserLogin class, you need to call super() first:UserLogin类的构造函数中,需要先调用super()

class UserLogin extends Auth {
    constructor(server, config) {
        super(); // you're missing this

        super.constructor(server);

        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 新的super.constructor是JavaScript中的有效表达式吗? - Is new super.constructor a valid expression in JavaScript? instanceof Parent 和 instanceof super.constructor 有什么区别? - What is the difference between instanceof Parent and instanceof super.constructor? ES6,必须在访问“ this”并在派生类中定义常量之前调用super - ES6, Must call super before accessing 'this' and defining constant in derived class Javascript继承:调用超级构造函数还是使用原型链? - Javascript inheritance: call super-constructor or use prototype chain? JavaScript继承和超级构造函数 - JavaScript inheritance and super constructor Javascript 中的 constructor() 和 super() 方法 - constructor() and super() methods in Javascript JavaScript $(&#39;li&#39;)。length调用引发未捕获的异常 - Javascript $('li').length call throws uncaught exception 来自javascript的ActionScript调用引发异常 - Actionscript call from javascript throws an exception 在javascript类中调用super之前先调用类函数 - Call class function before calling super in javascript class 打字稿错误:当类包含初始化的属性时,“超级”调用必须是构造函数中的第一条语句 - Typescript error : A 'super' call must be the first statement in the constructor when a class contains initialized properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM