ES6 - declare a prototype method on a class with an import statement

后端 未结 3 1180
野的像风
野的像风 2020-11-30 03:23

I am using ES6 classes. I want to be able to do this:

function Car(color) {
  this.color = color;
};

Car.prototype.getColor = require(\'./getColor\');
         


        
3条回答
  •  旧时难觅i
    2020-11-30 03:49

    If you are interested, I've developed a little module to bind functions with various number of parameters to class methods: https://github.com/ngryman/to-method.

    const toMethod = require('to-method');
    
    function Car(color) {
      this.color = color;
    }
    
    toMethod(Cat, { getColor: require('./getColor') });
    

提交回复
热议问题