Skip to content

Instantly share code, notes, and snippets.

@LOZORD
Created February 4, 2016 19:25
Show Gist options
  • Save LOZORD/1709ac63ddd15ab77f59 to your computer and use it in GitHub Desktop.
Save LOZORD/1709ac63ddd15ab77f59 to your computer and use it in GitHub Desktop.
Factory Example in ES6 (using Babel)
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