繁体   English   中英

ES6中的导出类不起作用

[英]Export class in ES6 not working

不知道如何使它工作。 我想在另一个文件中使用我的课程。 它找到了模块,但是当我尝试在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(节点样式)和ES6模块。 使用其中一个:

普通JS

GameRoom.js

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

ES6

GameRoom.js

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

GameRooms.js:

import GameRoom from './GameRoom.js'

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM