Created
February 4, 2016 19:25
-
-
Save LOZORD/1709ac63ddd15ab77f59 to your computer and use it in GitHub Desktop.
Factory Example in ES6 (using Babel)
This file contains hidden or 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
class Abstr { | |
constructor(f, o) { | |
this.f = f; | |
for (var key in o) { | |
this[key] = o[key]; | |
} | |
} | |
run () { | |
this.f(this); | |
} | |
} | |
let AbstrFactory = (f, o) => (() => new Abstr(f, o)); | |
let HW = AbstrFactory(() => console.log('hello world')); | |
let YS = AbstrFactory(() => console.log('yoloswag')); | |
let IntContainer = AbstrFactory( | |
(self) => { | |
self.val++; | |
console.log(self.val); | |
}, | |
{ | |
val: 0, | |
shout: () => console.log('YAY!') | |
} | |
); | |
let a = new HW(); | |
let b = new YS(); | |
let c = new IntContainer(); | |
a.run(); | |
b.run(); | |
c.run(); | |
c.run(); | |
c.shout(); | |
c.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment