How to add mixins to ES6 javascript classes?

前端 未结 3 834
再見小時候
再見小時候 2020-12-13 15:08

In an ES6 class with some instance variables and methods, how can you add a mixin to it? I\'ve given an example below, though I don\'t know if the syntax for the mixin objec

3条回答
  •  粉色の甜心
    2020-12-13 16:09

    You should probably look at Object.assign(). Gotta look something like this:

    Object.assign(Test.prototype, mixin);
    

    This will make sure all methods and properties from mixin will be copied into Test constructor's prototype object.

提交回复
热议问题