forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_objeto.js
More file actions
89 lines (74 loc) · 1.88 KB
/
06_objeto.js
File metadata and controls
89 lines (74 loc) · 1.88 KB
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
function speak(line) {
console.log(`The ${this.type} rabbit says '${line}'`);
}
var whiteRabbit = {type: "white", speak};
var hungryRabbit = {type: "hungry", speak};
var Rabbit = class Rabbit {
constructor(type) {
this.type = type;
}
speak(line) {
console.log(`The ${this.type} rabbit says '${line}'`);
}
}
var killerRabbit = new Rabbit("killer");
var blackRabbit = new Rabbit("black");
Rabbit.prototype.toString = function() {
return `a ${this.type} rabbit`;
};
var toStringSymbol = Symbol("toString");
var Matrix = class Matrix {
constructor(width, height, content = (x, y) => undefined) {
this.width = width;
this.height = height;
this.content = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
this.content[y * width + x] = content(x, y);
}
}
}
get(x, y) {
return this.content[y * this.width + x];
}
set(x, y, value) {
this.content[y * this.width + x] = value;
}
}
var MatrixIterator = class MatrixIterator {
constructor(matrix) {
this.x = 0;
this.y = 0;
this.matrix = matrix;
}
next() {
if (this.y == this.matrix.height) return {done: true};
let value = {x: this.x,
y: this.y,
value: this.matrix.get(this.x, this.y)};
this.x++;
if (this.x == this.matrix.width) {
this.x = 0;
this.y++;
}
return {value, done: false};
}
}
Matrix.prototype[Symbol.iterator] = function() {
return new MatrixIterator(this);
};
var SymmetricMatrix = class SymmetricMatrix extends Matrix {
constructor(size, content = (x, y) => undefined) {
super(size, size, (x, y) => {
if (x < y) return content(y, x);
else return content(x, y);
});
}
set(x, y, value) {
super.set(x, y, value);
if (x != y) {
super.set(y, x, value);
}
}
}
var matrix = new SymmetricMatrix(5, (x, y) => `${x},${y}`);