What's is the difference between mixin and extend, when to use each one?
The Mixin class allows you to create mixins, whose properties can be added to other classes. For instance, 1 2 3 4 5 6 7 8 9. import Mixin from '@ember/object/mixin'; const EditableMixin = Mixin.create({ edit() { console.log('starting to edit'); this.set('isEditing', true); }, isEditing: false });
Mixins are sometimes described as being "included" rather than "inherited". In short, the key difference from an inheritance is that mix-ins does NOT need to have a "is-a" relationship like in inheritance. From the implementation point of view, you can think it as an interface with implementations.
I wrote this article about Ember.Object which explains the differences in detail. Essentially, use extend
to create a new class from a base class and use mixins to separate lateral concerns that you may want to include in any number of classes / objects. Mixins can be included in classes via extend
or objects via create
.
A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With