-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter6-objects.js
107 lines (88 loc) · 2.25 KB
/
chapter6-objects.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
let Vector = function(x, y) {
this._x = x;
this._y = y;
};
Object.defineProperty(Vector.prototype, "x", {
get: function() { return this._x; },
set: function(x) { this._x = x; }
});
Object.defineProperty(Vector.prototype, "y", {
get: function() { return this._y; },
set: function(y) { this._y = y; }
});
Vector.prototype.plus = function(vector) {
let x = this.x + vector.x;
let y = this.y + vector.y;
let sumVector = new Vector(x, y);
return sumVector;
};
Vector.prototype.minus = function(vector) {
let x = this.x - vector.x;
let y = this.y - vector.y;
let diffVector = new Vector(x, y);
return diffVector;
};
Vector.prototype.distance = function() {
let origin = new Vector(0, 0);
let difference = this.minus(origin);
let sum = Math.pow(difference.x, 2) + Math.pow(difference.y, 2);
let result = Math.sqrt(sum);
return result;
};
let StretchCell = function (inner, width, height) {
this._inner = inner;
this._width = width;
this._height = height;
};
StretchCell.prototype.minWidth = function() {
return Math.max(this._width, this._inner.minWidth());
};
StretchCell.prototype.minHeight = function() {
return Math.max(this._height, this._inner.minHeight());
};
StretchCell.prototype.draw = function(width, height) {
return this._inner.draw(width, height);
};
let ArraySeq = function(array) {
this.pos = -1;
this.array = array;
};
ArraySeq.prototype.next = function() {
this.pos ++;
if (this.pos < this.array.length) {
return true;
}
return false;
};
ArraySeq.prototype.current = function() {
return this.array[this.pos];
};
let RangeSeq = function(from, to) {
this.pos = from - 1;
this.to = to;
};
RangeSeq.prototype.next = function() {
if (this.pos < this.to){
this.pos++;
return true;
}
return false;
};
RangeSeq.prototype.current = function() {
return this.pos;
};
function logFive(sequence) {
let arr = [];
for (var i = 0; i < 5; i++) {
if (!sequence.next())
break;
arr.push(sequence.current());
}
return arr;
}
exports.Vector = Vector;
exports.StretchCell = StretchCell;
exports.RangeSeq = RangeSeq;
exports.ArraySeq = ArraySeq;
exports.logFive = logFive;