Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Created January 14, 2012 23:46
Show Gist options
  • Save iaincarsberg/1613428 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1613428 to your computer and use it in GitHub Desktop.
Entity = Compose(
function () {
// fake a unique id
this.id = Math.floor(Math.random() * 1000);
},
{
a: function () {
return this.id;
}
}
);
// Add some functionality to the Entity
Entity.expand(Compose(
function (custom) {
this.name = (custom || {}).name || 'unknown';
},
{
getName: function () {
return this.name + ' is entity ' + this.id;
}
}
));
var e1 = new Entity({name: 'Iain'}),
e2 = new Entity({name: 'Carsberg'}),
e3 = new Entity();
console.log(e1.id, e1.a(), e1.getName());// 918 918 "Iain is entity 918"
console.log(e2.id, e2.a(), e2.getName());// 919 919 "Carsberg is entity 919"
console.log(e3.id, e3.a(), e3.getName());// 895 895 "unknown is entity 895"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment