-1

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 ?

Nicolas Frbezar
  • 497
  • 1
  • 5
  • 24

1 Answers1

2

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 ....;
    }
}
Daniel Tran
  • 6,083
  • 12
  • 25