Last active
February 12, 2017 22:54
-
-
Save sri-ni/076be9963d4dde1a1cd89710340bb5ee to your computer and use it in GitHub Desktop.
JS 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
// Mixin Pattern | |
// ------------- | |
// Class / Sub-Class concept | |
var Person = function(firstName, lastName, gender) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.gender = gender || 'male'; | |
} | |
var clark = new Person('Clark', 'Kent', 'male'); | |
console.log(clark); | |
var SuperHero = function(firstName, lastName, gender, powers) { | |
Person.call(this, firstName, lastName, gender); | |
this.powers = powers; | |
}; | |
SuperHero.prototype = Object.create(Person.prototype); | |
console.log(SuperHero); | |
var manofsteel = new SuperHero('Man', 'of Steel', 'male', ['super strength', 'heat vision']); | |
console.log(manofsteel); | |
var wonderwoman = new SuperHero('Wonder', 'Woman', 'female', ['shield', 'sword', 'bracelet']); | |
console.log(wonderwoman); | |
// Mixins | |
var myMixins = { | |
moveLeft: function() { | |
console.log("move left"); | |
}, | |
moveRight: function() { | |
console.log("move right"); | |
}, | |
stop: function() { | |
console.log("stop"); | |
} | |
} | |
function CarAnimator() { | |
this.moveUp = function() { | |
console.log("move up"); | |
} | |
} | |
function PersonAnimator() { | |
this.moveRandom = function() { | |
console.log("move random"); | |
} | |
} | |
function extend(tar, sou) { | |
for (var key in sou) { | |
tar[key] = sou[key]; | |
} | |
} | |
extend(CarAnimator.prototype, myMixins); | |
extend(PersonAnimator.prototype, myMixins); | |
console.log(CarAnimator); | |
console.log(PersonAnimator); | |
var myAnimator = new CarAnimator(); | |
console.log(myAnimator); | |
myAnimator.moveUp(); | |
myAnimator.moveRight(); | |
myAnimator.stop(); | |
// Mixins - selective | |
var Car = function(settings) { | |
this.model = settings.model || "Audi"; | |
this.color = settings.color || "grey"; | |
}; | |
var Mixin = function() {}; | |
Mixin.prototype = { | |
driveForward: function() { | |
console.log('Drive Forward'); | |
}, | |
driveBackward: function() { | |
console.log('Drive Backward'); | |
}, | |
driveSideward: function() { | |
console.log('Drive Sideward'); | |
} | |
}; | |
function augment(receiving, giving) { | |
if (arguments[2]) { | |
for (i=2; i<arguments.length; i++) { | |
receiving.prototype[arguments[i]] = giving.prototype[arguments[i]]; | |
} | |
} else { | |
for (var ability in giving.prototype) { | |
if (!Object.hasOwnProperty.call(receiving.prototype, ability)) { | |
receiving.prototype[ability] = giving.prototype[ability]; | |
} | |
} | |
} | |
} | |
augment(Car, Mixin, 'driveForward', 'driveBackward'); | |
var bmw = new Car({ | |
model: "BMW", | |
color: "silver" | |
}); | |
bmw.driveForward(); | |
augment(Car, Mixin); | |
var tesla = new Car({ | |
model: "Tesla", | |
color: "red" | |
}); | |
tesla.driveSideward(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment