Implement all the methods of an object into another object
// Create a couple of test entities with ids
var entity1 = new IgeEntity().id('entity1'),
entity2 = new IgeEntity().id('entity2');
// Let's define an object with a couple of methods
var obj = {
newMethod1: function () {
console.log('method1 called on object: ' + this.id());
},
newMethod2: function () {
console.log('method2 called on object: ' + this.id());
}
};
// Now let's implement the methods on our entities
entity1.implement(obj);
entity2.implement(obj);
// The entities now have the newMethod1 and newMethod2
// methods as part of their instance so we can call them:
entity1.newMethod1();
// The output to the console is:
// method1 called on object: entity1
// Now let's call newMethod2 on entity2:
entity2.newMethod2();
// The output to the console is:
// method2 called on object: entity2
// As you can see, this is a great way to add extra modular
// functionality to objects / entities at runtime.