-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixin.js
45 lines (37 loc) · 1.08 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
(function () {
'use strict';
function mixin() {
var prop,
newPrototype = {},
args = Array.prototype.slice.call(arguments),
baseObject = args[0],
prototypes = args.slice(1);
prototypes.forEach(function (item) {
for(prop in item) {
if(item.hasOwnProperty(prop)) {
newPrototype[prop] = item[prop];
}
}
});
baseObject.prototype = newPrototype;
baseObject.prototype.constructor = baseObject;
}
function Shape2d() {}
Shape2d.prototype.getArea = function () {
return this.sides[0] * this.sides[1];
}
function Position() {}
Position.prototype.getCoords = function () {
return this.coords.join(', ');
}
function Rectangle(width, height, top, left) {
this.sides = [width, height];
this.coords = [top, left];
}
mixin(Rectangle, Shape2d.prototype, Position.prototype);
var rectangle = new Rectangle(5, 6, 100, 200);
console.log(rectangle.constructor.name);
console.log(rectangle.sides.length);
console.log(rectangle.getArea());
console.log(rectangle.getCoords());
}());