Last active
August 29, 2015 14:21
-
-
Save jsteenkamp/9e0f0e1b037dac0485b6 to your computer and use it in GitHub Desktop.
Better to use Prototypal OO and not ES6 class (Angular example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import _ from 'lodash'; | |
// generate some list items | |
let items = _.times(10).map((n) => { | |
return { | |
title: `This is log item ${n + 1}`, | |
description: `This is item description ${n + 1}` | |
}; | |
}); | |
// ES6 classes are bad news due to inheritance hierarchies!!! | |
class Service { | |
constructor(LayoutService) { | |
this.LayoutService = LayoutService; | |
} | |
} | |
// sub-classes lead to arthritic code and brittleness due to rippling side-effects | |
class Controller extends Service { | |
constructor() { | |
// super is a code smell | |
super(); | |
this.items = items; | |
} | |
} | |
/* | |
Better to use a factory function, here we use a delegate prototype | |
where LayoutService is also be a factory function | |
for details see @_ericelliot | |
*/ | |
let controller = (LayoutService) => { | |
return Object.assign(LayoutService, { | |
items: items | |
}); | |
}; | |
// angular DI - just to show how it would work, export Controller will work her as well | |
export default ['LayoutService', controller]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment