Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ember.js, what's the difference between mixin and extend?

Tags:

What's is the difference between mixin and extend, when to use each one?

like image 436
NkS Avatar asked Nov 24 '12 07:11

NkS


People also ask

What is a mixin in Ember?

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 });

What is the difference between a mixin and inheritance?

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.


2 Answers

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.

like image 198
Dan Gebhardt Avatar answered Oct 26 '22 00:10

Dan Gebhardt


A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:

  1. You want to provide a lot of optional features for a class.
  2. You want to use one particular feature in a lot of different classes.
like image 40
raghul Avatar answered Oct 25 '22 22:10

raghul