-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixin.js
32 lines (27 loc) · 933 Bytes
/
mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var Car = function (settings) {
this.model = settings.model || 'no model provided'; this.colour = settings.colour || 'no colour provided';
};
var Mixin = function () { };
Mixin.prototype = {
driveForward: function () {
console.log('drive forward');
},
driveBackward: function () {
console.log('drive backward');
}
};
function augment(receivingClass, givingClass) {
if (arguments[2]) {
for (var i = 2, len = arguments.length; i < len; i++) { receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]]; }
} else {
for (var methodName in givingClass.prototype) {
if (!receivingClass.prototype[methodName]) {
receivingClass.prototype[methodName] = givingClass.prototype[methodName];
}
}
}
}
augment(Car, Mixin, 'driveForward', 'driveBackward');
var vehicle = new Car({ model: 'Ford Escort', colour: 'blue' });
vehicle.driveForward();
vehicle.driveBackward();