gpt4 book ai didi

javascript - ES6 中的导出类不起作用

转载 作者:行者123 更新时间:2023-12-03 08:40:02 25 4
gpt4 key购买 nike

不知道如何让它工作。我想在另一个文件中使用我的类(class)。它找到了该模块,但是当我尝试在 GameRooms.js 中实例化一个新的 GameRoom 时,它说 GameRoom 不是一个函数。 GameRooms.js 中的控制台日志打印未定义。


GameRooms.js

'use strict';

var GameRoom = require('./game_room.server.controller').GameRoom;
var GameRooms = new Map();
console.log(GameRoom);

exports.createGame = function () {
new GameRoom([1,2,3,4,5,6], true);
};

exports.createGame();

GameRoom.js

'use strict';

var PublicGameStates = require('./states/public_game.server.controller');
var PrivateGameStates = require('./states/private_game.server.controller');
var GameSocket = require('../sockets/game.server.socket.config');


export class GameRoom {

constructor(players, public_room) {
// Set players prop, return error if not array
if (Array.isArray(players)) this.players = players;
else throw 'Attempted to create game without correct players param';

// Set min and max players
this.min_players = 3;
this.max_players = 8;
// Set to true if judge leaves
this.no_winner = false;
// Setup states, private or public
this.States = public_room ? PublicGameStates : PrivateGameStates;
// Create a new socket channel for the room and connect the players
this.GameSocket = new GameSocket(this.players);

// Player disconnects from socket
this.GameSocket.on('player disconnect', function(player) {
this.playerDisconnects(player);
});

// TODO: Handle socket ready timeout
this.GameSocket.on('socket ready', function(player) {
// Set Initial State
if (public_room) this.CurrState = new this.States.GameEstablished();
});
}

set gameState(State) {
this.CurrState = State;
}

get gameState() {
return this.CurrState;
}

// When a player presses leave game
playerExits(playerToRemove) {
// Delete player from players array
this.players.splice(this.players.indexOf(playerToRemove), 1);

// If theres not enough players to continue, terminate game
if (this.players.length < this.min_players) this.CurrState = 'TERMINATE';
// Enough players to keep game open
else {
// Disconnect the player from the game socket
this.game_socket.kickPlayer(playerToRemove);
// The judge left the game, no winner can be determined
// TODO: Flag judge for leaving mid-game!
if (playerToRemove === this.judge) this.no_winner = true;
}
}

// When a player disconnects without warning, e.g. closes window
playerDisconnects(playerToRemove) {
// Delete player from players array
this.players.splice(this.players.indexOf(playerToRemove), 1);
// If theres not enough players to continue, terminate game
if (this.players.length < this.min_players) this.CurrState = 'TERMINATE';
// Enough players to keep game room open
else {
// The judge left the game, no winner can be determined
// TODO: Flag judge for leaving mid-game!
if (playerToRemove === this.judge) this.no_winner = true;
}
}
}

最佳答案

您混合了 CommonJS(Node 样式)和 ES6 模块。使用其中之一:

CommonJS

GameRoom.js

exports.GameRoom = class GameRoom { /* ... * / };

ES6

GameRoom.js

export default class GameRoom { /* ... * / };

GameRooms.js:

import GameRoom from './GameRoom.js'

关于javascript - ES6 中的导出类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33069325/

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