Created
July 2, 2015 09:40
-
-
Save hwclass/e60bb31d069213db5b81 to your computer and use it in GitHub Desktop.
Design Patterns : Mixin Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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