JavaScript 静态 class

Javascrip static class

我目前正在开发一个只包含静态字段和方法的 class,因为它不需要保持任何状态(它只是一个解析消息的 class 和 运行是其中包含的命令)。

当我需要 class 和 console.log 时,我得到了它应有的所有属性,但是当我一一调用它们时,它们都显示为未定义:

我记录 class 时的输出:

const Executor = require('./executor.js')
console.log(Executor.COMMANDS)
OUTPUT:
{
    Executor: [class Executor] {
      COMMANDS: {
        none: -1,
        follow: 1,
        dig_area: 2,
        silent_mode: 3,
        help: 4,
        tp: 5
      },
      run: [Function: run]
    }
}

这里是实际的 class:


class Executor
{
    /*
    All fields and methods are static as they shouldn't hold any state variable
    given that all commands must run independently
    */
    
    static COMMANDS = {

        none: -1,
        follow: 1,
        dig_area: 2,
        silent_mode: 3,
        help: 4,
        tp: 5

    };
    
    
    

    static run = (message, username) => {
        
        const { command, body } = Parser.parse(message);
        console.log(`Command: ${command}\t Body: ${body}`)
        
        switch(command)
        {
            case COMMANDS.follow:
                break;
            
            case COMMANDS.dig_area:
                break;

            case COMMANDS.silent_mode:
                break;

            case COMMANDS.help:
                break;

            case COMMANDS.tp:
                break;

            case COMMANDS.none:
                break;

        }
    } 
}



class Parser
{
    //Prefix which will preceed any command given to the boy
    static PREFIX = '!';

    //List of command values to pass to the Command Executor

    static COMMANDS = {

        
        'follow': Executor.COMMANDS.follow,
        'dig_area': Executor.COMMANDS.dig_area,
        'silent_mode': Executor.COMMANDS.silent_mode,
        'help': Executor.COMMANDS.help,
        'tp': Executor.COMMANDS.tp
        
    }

    
    //Should return the whole message array but the last aparison
    static parse(message, username)
    {
        StringArr = message.split(' ');
        isCommand =  StringArr.shift() === this.PREFIX;
        
        if(!isCommand) return -1
        
        return { command: COMMANDS[StringArr[0]], body: StringArr};
    }

}


module.exports.Executor = Executor;

我不知道为什么它会正确加载 class 但每当我访问任何属性(函数或字段)时它都会变得未定义。此外,当将 运行 定义为像这样的常用函数时,它没有将其检测为 class:

的 属性

class Executor
{
    /*
    All fields and methods are static as they shouldn't hold any state variable
    given that all commands must run independently
    */

    static COMMANDS = {

        none: -1,
        follow: 1,
        dig_area: 2,
        silent_mode: 3,
        help: 4,
        tp: 5

    };




    static run(message, username){

        const { command, body } = Parser.parse(message);
        console.log(`Command: ${command}\t Body: ${body}`)

        switch(command)
        {
            case COMMANDS.follow:
                break;

            case COMMANDS.dig_area:
                break;

            case COMMANDS.silent_mode:
                break;

            case COMMANDS.help:
                break;

            case COMMANDS.tp:
                break;

            case COMMANDS.none:
                break;

        }
    } 

如有任何帮助,我们将不胜感激。谢谢大家!

为什么需要“静态 class”?

您所说的只是一个导出具有以下属性的对象的模块:

const COMMANDS = {
  follow      :  1 ,
  dig_area    :  2 ,
  silent_mode :  3 ,
  help        :  4 , 
  tp          :  5
}

const Parser = {

  //Prefix which will preceed any command given to the boy
  PREFIX: '!',

  //List of command values to pass to the Command Executor
  COMMANDS: {...COMMANDS},

  //Should return the whole message array but the last aparison
  parse(message, username) {
    const [ pfx , cmd, ...body ] = message.split(' ');
    const isCommand = pfx === this.PREFIX;
    return isCommand ? { command: this.COMMANDS[cmd], body } : -1;
  }

}

const Executor = {

  COMMANDS: { none: -1, ...COMMANDS},

  run: (message, username) => {
    const { command, body } = Parser.parse(message);

    console.log(`Command: ${command}\t Body: ${body}`)

    switch (command) {
      case this.COMMANDS.follow:      break;
      case this.COMMANDS.dig_area:    break;
      case this.COMMANDS.silent_mode: break;
      case this.COMMANDS.help:        break;
      case this.COMMANDS.tp:          break;
      case this.COMMANDS.none:        break;
    }

  }

};
exports = modules.exports = Executor;

如果你确保它是不可变的,那么Object.freeze()它:

const Executor = Object.freeze({

  COMMANDS: Object.freeze({ none: -1, ...COMMANDS}),

  run: (message, username) => {
    . . .
  },

});