Design Patterns : Mixin Pattern
/** | |
* @fileoverview The file that is to be included a view and start working | |
* @author hwclass | |
*/ | |
/** | |
* base.js | |
* This file is used to initialize some extended object references | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** @type {Object} */ | |
var person = new Person('Tacchinardi', 33); | |
person.getName(); // logs Tacchinardi | |
person.getAge(); // logs 33 |
/** | |
* @fileoverview The file that some object extending processes | |
* @author hwclass | |
*/ | |
/** | |
* init.js | |
* This file is used to extend the functionalities | |
* of the object references with additionals | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
extend(Person.prototype, mixin.person); |
/** | |
* @fileoverview The file that contains some mixins | |
* @author hwclass | |
*/ | |
/** | |
* mixin.js | |
* This file contains an object literal | |
* that contains some functionalities for some objects | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* mixin object | |
* @noparam | |
*/ | |
var mixin = { | |
person : { | |
getName : function () { | |
return this.name | |
}, | |
getAge : function () { | |
return this.age; | |
} | |
} | |
} |
/** | |
* @fileoverview The sample js file for Person object | |
* @author hwclass | |
*/ | |
/** | |
* Person.js | |
* This file contains an object to be a reference for new instances | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* Person object | |
* @param {String} name | |
* @param {Function} context | |
*/ | |
var Person = function (name, age) { | |
this.name = name; | |
this.age = age; | |
} |
/** | |
* @fileoverview The file that contains some utilities | |
* @author hwclass | |
*/ | |
/** | |
* utils.js | |
* This file contains a method that extend a reference object | |
* with some additional functionalities | |
*/ | |
'use strict'; | |
//----------------------------------------------------------- | |
// Public | |
//----------------------------------------------------------- | |
/** | |
* extend method | |
* @param {Object} destination | |
* @param {Object} source | |
*/ | |
function extend(destination, source) { | |
for (var k in source) { | |
if (source.hasOwnProperty(k)) { | |
destination[k] = source[k]; | |
} | |
} | |
return destination; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment