I had been looking for a good way to do mixins in ES6.
Looks like I finally found one.
http://raganwald.com/2015/06/10/mixins.html
Essentially,
create the mixin as a const
Use Object.assign to add the properties to the Class
Looks like I finally found one.
http://raganwald.com/2015/06/10/mixins.html
Essentially,
create the mixin as a const
Use Object.assign to add the properties to the Class
const BookCollector = {
addToCollection (name) {
this.collection().push(name);
return this;
},
collection () {
return this._collected_books || (this._collected_books = []);
}
};
class Person {
constructor (first, last) {
this.rename(first, last);
}
fullName () {
return this.firstName + " " + this.lastName;
}
rename (first, last) {
this.firstName = first;
this.lastName = last;
return this;
}
};
Object.assign(Person.prototype, BookCollector);
(I am copying and pasting this for personal reference in case the original site link goes down).
Comments