Skip to content

Instantly share code, notes, and snippets.

@cerebrl
Created August 2, 2015 03:17
Show Gist options
  • Save cerebrl/9913fec26b5beb5e1a4d to your computer and use it in GitHub Desktop.
Save cerebrl/9913fec26b5beb5e1a4d to your computer and use it in GitHub Desktop.
Playing with factories, prototypes, behavior delegation, mixins and more: http://cerebrl.jsbin.com/zaxaqo/edit?js,console …. I love JavaScript!
/** ****************************************
* Utility file
* mixin.js
* @returns [object]
*/
function mixinFactory() {
var len = arguments.length,
i = 0,
finalObj = {};
function copyMethods(obj) {
var j = 0,
keys = Object.keys(obj),
len = keys.length;
for (j; j < len; j++) {
// Check if value is a function
if (obj[keys[j]].call && obj[keys[j]].apply) {
finalObj[keys[j]] = obj[keys[j]];
}
}
}
for (i; i < len; i++) {
// Check if value is an object
if (arguments[i] === Object(arguments[i])) {
copyMethods(arguments[i]);
}
}
return finalObj;
}
/** ****************************************
* App level module for say hi
* sayhi.js
* @returns [object]
*/
function sayHiFactory() {
return {
sayHi: function sayHi(name) {
name = name || this.name;
console.log(name + ' says, \'hallo\'!');
}
};
}
/** ****************************************
* App level module for say bye
* saybye.js
* @returns [object]
*/
function sayByeFactory() {
return {
sayBye: function sayBye(name) {
name = name || this.name;
console.log(name + ' says, \'bye bye\'!');
}
};
}
/** ****************************************
* Feature level module/model for say hi
* feature.js
*/
/**
* Create prototypal inheritence pattern
*/
// "Require" from modules to create prototype ready objects
var protoHi = sayHiFactory(),
protoBye = sayByeFactory();
// Create instances with prototypal inheritence
var instanceOO = Object.create(protoHi, {name: {value: 'instanceOO'}}),
instanceFP = Object.create(protoHi);
/**
* Examples of objects with simple "copied" behavior
*/
// Here you can just straight call the factory to the end object
var copyOO = sayHiFactory(),
copyFP = sayHiFactory();
copyOO.name = 'copyOO';
/**
* Create of objects from multiple other objects with prototypal inheritence
*/
// First create the mixin prototype ready object
var protoHiBye = mixinFactory(protoHi, protoBye, 'thing');
// Now create the instances from it with prototypal inheritence
var mixinInstanceOO = Object.create(protoHiBye, {name: {value: 'mixinInstanceOO'}}),
mixinInstanceFP = Object.create(protoHiBye);
/**
* Create objects from multiple other objects with "copied" behavior
*/
// Here too, you can decide to straight call the factor to the end object
var mixinCopyOO = mixinFactory(sayHiFactory(), sayByeFactory()),
mixinCopyFP = mixinFactory(sayHiFactory(), sayByeFactory());
mixinCopyOO.name = 'mixinCopyOO';
/**
* Prove prototypal inheritence versus copied
*/
console.log('-------------------------------\n' +
'Prove prototypal inheritence:\n' +
'-------------------------------');
console.log('instances use prototypes? ' +
(instanceOO.sayHi === instanceFP.sayHi));
console.log('copies use prototypes? ' +
(copyOO.sayHi === copyFP.sayHi));
console.log('mixinInstances use prototypes? ' +
(mixinInstanceOO.sayHi === mixinInstanceFP.sayHi));
console.log('mixinCopies use prototypes? ' +
(mixinCopyOO.sayHi === mixinCopyFP.sayHi));
/**
* Prove behavior
*/
console.log('\n-------------------------------\n' +
'Prove actual method execution:\n' +
'-------------------------------');
instanceOO.sayHi();
instanceFP.sayHi('instanceFP');
copyOO.sayHi();
copyFP.sayHi('copyFP');
mixinInstanceOO.sayHi();
mixinInstanceFP.sayBye('mixinInstanceFP');
mixinCopyOO.sayHi();
mixinCopyFP.sayBye('mixinCopyFP');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment