Factory example with es6 template strings
Factory example with es6 template strings
by Nirvanachain
HTML
<script src="https://rawgit.com/lodash/lodash/3.7.0/lodash.min.js"></script>
<div class="js-container"></div>
JavaScript
//animal is the delegate prototype. Like a constructor function
var animal = {
animalType: 'animal',
describe: function () {
return `An ${this.animalType} with ${this.furColor} fur,
${this.legs} legs, and a ${this.tail} tail. Job: ${this.position()}`;
}
};
//Factory, inherits from animal.
var mouseFactory = function mouseFactory () {
var secret = 'secret agent'; //private variable
//LoDash .assign() is equivelent to es6 Object.assign() and $.extend
return _.assign(Object.create(animal), {
animalType: 'mouse',
furColor: 'brown',
legs: 4,
tail: 'long, skinny',
position: function () {
return secret;
},
add: function () {
document.querySelector('.js-container').innerHTML = this.describe();
}
});
};
var mickey = mouseFactory();
console.log( mickey.describe() );
//Add template string to the DOM
mickey.add();