ES6 class add object of functions

243 Views Asked by At

I'm trying to include an object of function as a static class properties, to call like myClass.setPayload[id].call(this, buffer).

Actually, i have this :

var setPayload = {
    0x01: function(buffer){...},
    0x0a: function (buffer) {...}
}

class myClass{
    constructor(buffer){
        ...
        setPayload[buffer[1]].call(this, buffer.slice(...))
    }
}

The objective is to suppress the nedd of constructor, so i try the following, that not work :

class myClass{
    static setPayload = {
         0x01: function(buffer){...},
         0x0a: function(buffer){...}
    }
}

Suggestions ?

1

There are 1 best solutions below

0
On BEST ANSWER

Static is only used to define a method for a class - see static - JavaScript You can either set:

MyClass.setPayLoad = ...

// or make a getter function:
class MyClass {
    static get setPayLoad() {
         return ....;
    }
}