gpt4 book ai didi

JavaScript super.constructor() 抛出 "must call before accessing this"异常

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:07 24 4
gpt4 key购买 nike

我正在尝试在 JavaScript 中派生一个类。基本上我有一个类 Auth (更像是一个接口(interface))和一个从它扩展的类 UserLogin 。问题是,即使我在其他任何事情之前调用 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

基类:

/*
* 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;
}
...

UserLogin 类 - 如您所见,在调用 super 构造函数之前我没有调用 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;
}
...

我的启动脚本:

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();

我试图在线寻找答案,但我能找到的只是有关如何在 JavaScript 中派生类的页面。

有什么想法吗?

最佳答案

UserLogin类的构造函数中,您需要调用super()第一:

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

super.constructor(server);

// ...
}
}

关于JavaScript super.constructor() 抛出 "must call before accessing this"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59932919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com